Android顶部工具栏和底部工具栏的简单实现代码
废话少说,直接上图,有图有真相。
这两个工具栏全是用布局来实现的。底部工具栏布局代码:
代码
< xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<android:id="@+id/edit"
android:text="测试"
android:gravity="center_horizontal"
android:drawabletop="@drawable/ic_dialog_alert"
android:layout_alignparentleft="true"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
<android:id="@+id/menu2"
android:text="测试"
android:gravity="center_horizontal"
android:drawabletop="@drawable/ic_dialog_dialer"
android:layout_torightof="@id/edit"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
<android:id="@+id/effect"
android:text="测试"
android:gravity="center_horizontal"
android:drawabletop="@drawable/ic_dialog_email"
android:layout_torightof="@id/menu2"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
<android:id="@+id/menu3"
android:text="测试"
android:gravity="center_horizontal"
android:drawabletop="@drawable/ic_dialog_info"
android:layout_torightof="@id/effect"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
顶部工具栏布局代码:
代码
<xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/top"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<android:id="@+id/undo"
android:src="@drawable/ic_dialog_map"
android:layout_alignparentleft="true"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
<android:id="@+id/save"
android:src="@drawable/ic_dialog_menu_generic"
android:layout_torightof="@id/undo"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
<android:id="@+id/share"
android:src="@drawable/ic_dialog_time"
android:layout_torightof="@id/save"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
<android:id="@+id/redo"
android:src="@drawable/ic_dialog_usb"
android:layout_torightof="@id/share"
android:layout_width="80dip"
android:layout_height="wrap_content"
>
主窗口布局代码:
代码
<android:layout_height="fill_parent">
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_topmenu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignparenttop="true"
>
<android:text="asdfasdfas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layout_topmenu">
<xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignparentbottom="true"
>
主窗口代码:
代码
package com.figoyu.photoqualify;
import android.app.activity;
import android.os.bundle;
import android.view.motionevent;
import android.view.view;
import android.view.window;
import android.view.view.ontouchlistener;
import android.widget.imageview;
import android.widget.textview;
public class mainactivity extends activity {
textview txt1;
textview edittv;
imageview imgbtn;
boolean isfirst = true;
boolean istopfirst = true;
@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
requestwindowfeature(window.feature_no_title);
setcontentview(r.layout.main);
edittv = (textview)findviewbyid(r.id.edit);
txt1 = (textview)findviewbyid(r.id.txt1);
imgbtn = (imageview)findviewbyid(r.id.undo);
edittv.setontouchlistener(new ontouchlistener() {
@override
public boolean ontouch(view v, motionevent event) {
if(isfirst) {
txt1.settext("编辑");
edittv.setbackgrounddrawable(getresources().getdrawable(r.drawable.item_background));
isfirst = false;
}else {
txt1.settext("退出编辑");
edittv.setbackgrounddrawable(getresources().getdrawable(r.drawable.touming));
isfirst = true;
}
return false;
}
});
imgbtn.setontouchlistener(new ontouchlistener() {
@override
public boolean ontouch(view v, motionevent event) {
if(istopfirst) {
txt1.settext("编辑");
istopfirst = false;
}else {
txt1.settext("退出编辑");
istopfirst = true;
}
return false;
}
});
}
}