android实现底部菜单栏

android程序,许多时候需要菜单栏显示在底部或顶部,但是没有很好的组建供程序员调用,过自定义了一个视图,用来实现底部菜单栏,效果如下图:

实现的主要代码:

Java代码
  1. @Override   
  2.     public void onCreate(Bundle savedInstanceState) {   
  3.         super.onCreate(savedInstanceState);   
  4.         requestWindowFeature(Window.FEATURE_NO_TITLE);   
  5.         setContentView(R.layout.main);   
  6.   
  7.         linearLayout = (LinearLayout) findViewById(R.id.home);   
  8.         linearLayout.setOnClickListener(new OnClickListener() {   
  9.             @Override   
  10.             public void onClick(View v) {   
  11.                 linearLayout   
  12.                         .setBackgroundResource(R.drawable.tab_two_highlight);   
  13.                 publish.setBackgroundResource(R.drawable.tab_one_normal);   
  14.                 change.setBackgroundResource(R.drawable.tab_one_normal);   
  15.                 more.setBackgroundResource(R.drawable.tab_one_normal);   
  16.             }   
  17.         });   
  18.         linearLayout   
  19.         .setBackgroundResource(R.drawable.tab_two_highlight);   
  20.   
  21.         publish = (LinearLayout) findViewById(R.id.publish);   
  22.         publish.setOnClickListener(new OnClickListener() {   
  23.             @Override   
  24.             public void onClick(View v) {   
  25.                 publish.setBackgroundResource(R.drawable.tab_two_highlight);   
  26.                 linearLayout.setBackgroundResource(R.drawable.tab_one_normal);   
  27.                 change.setBackgroundResource(R.drawable.tab_one_normal);   
  28.                 more.setBackgroundResource(R.drawable.tab_one_normal);   
  29.             }   
  30.         });   
  31.   
  32.         change = (LinearLayout) findViewById(R.id.change);   
  33.         change.setOnClickListener(new OnClickListener() {   
  34.             @Override   
  35.             public void onClick(View v) {   
  36.                 change.setBackgroundResource(R.drawable.tab_two_highlight);   
  37.                 linearLayout.setBackgroundResource(R.drawable.tab_one_normal);   
  38.                 publish.setBackgroundResource(R.drawable.tab_one_normal);   
  39.                 more.setBackgroundResource(R.drawable.tab_one_normal);   
  40.             }   
  41.         });   
  42.   
  43.         more = (LinearLayout) findViewById(R.id.more);   
  44.         more.setOnClickListener(new OnClickListener() {   
  45.             @Override   
  46.             public void onClick(View v) {   
  47.                 more.setBackgroundResource(R.drawable.tab_two_highlight);   
  48.                 linearLayout.setBackgroundResource(R.drawable.tab_one_normal);   
  49.                 publish.setBackgroundResource(R.drawable.tab_one_normal);   
  50.                 change.setBackgroundResource(R.drawable.tab_one_normal);   
  51.             }   
  52.         });   
  53.     }   
@Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         requestWindowFeature(Window.FEATURE_NO_TITLE);         setContentView(R.layout.main);         linearLayout = (LinearLayout) findViewById(R.id.home);         linearLayout.setOnClickListener(new OnClickListener() {             @Override             public void onClick(View v) {                 linearLayout                         .setBackgroundResource(R.drawable.tab_two_highlight);                 publish.setBackgroundResource(R.drawable.tab_one_normal);                 change.setBackgroundResource(R.drawable.tab_one_normal);                 more.setBackgroundResource(R.drawable.tab_one_normal);             }         });         linearLayout         .setBackgroundResource(R.drawable.tab_two_highlight);         publish = (LinearLayout) findViewById(R.id.publish);         publish.setOnClickListener(new OnClickListener() {             @Override             public void onClick(View v) {                 publish.setBackgroundResource(R.drawable.tab_two_highlight);                 linearLayout.setBackgroundResource(R.drawable.tab_one_normal);                 change.setBackgroundResource(R.drawable.tab_one_normal);                 more.setBackgroundResource(R.drawable.tab_one_normal);             }         });         change = (LinearLayout) findViewById(R.id.change);         change.setOnClickListener(new OnClickListener() {             @Override             public void onClick(View v) {                 change.setBackgroundResource(R.drawable.tab_two_highlight);                 linearLayout.setBackgroundResource(R.drawable.tab_one_normal);                 publish.setBackgroundResource(R.drawable.tab_one_normal);                 more.setBackgroundResource(R.drawable.tab_one_normal);             }         });         more = (LinearLayout) findViewById(R.id.more);         more.setOnClickListener(new OnClickListener() {             @Override             public void onClick(View v) {                 more.setBackgroundResource(R.drawable.tab_two_highlight);                 linearLayout.setBackgroundResource(R.drawable.tab_one_normal);                 publish.setBackgroundResource(R.drawable.tab_one_normal);                 change.setBackgroundResource(R.drawable.tab_one_normal);             }         });     } 

配置文件实现:

Xml代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:orientation="horizontal" android:layout_width="fill_parent"   
  4.     android:layout_height="wrap_content">   
  5.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  6.         android:orientation="vertical" android:layout_width="80px"   
  7.         android:layout_height="wrap_content" android:id="@+id/home"   
  8.         android:background="@drawable/tab_one_normal">   
  9.         <TextView android:layout_width="wrap_content"   
  10.             android:layout_height="wrap_content" android:text="@string/home"   
  11.             android:gravity="center" android:paddingTop="29px"   
  12.             android:background="@drawable/home" />   
  13.     </LinearLayout>   
  14.     <ImageView android:layout_width="wrap_content"   
  15.             android:layout_height="wrap_content"  android:src="@drawable/line" />   
  16.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  17.         android:orientation="vertical" android:layout_width="80px"   
  18.         android:layout_height="wrap_content" android:background="@drawable/tab_one_normal"   
  19.         android:id="@+id/publish">   
  20.         <TextView android:layout_width="wrap_content"   
  21.             android:layout_height="wrap_content" android:text="@string/publish"   
  22.             android:gravity="center" android:paddingTop="29px"   
  23.              android:background="@drawable/publish" />   
  24.     </LinearLayout>   
  25.     <ImageView android:layout_width="wrap_content"   
  26.             android:layout_height="wrap_content"  android:src="@drawable/line" />   
  27.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  28.         android:orientation="vertical" android:layout_width="80px" android:id="@+id/change"   
  29.         android:layout_height="wrap_content" android:background="@drawable/tab_one_normal">   
  30.         <TextView android:layout_width="wrap_content"   
  31.             android:layout_height="wrap_content" android:text="@string/change"   
  32.             android:gravity="center" android:paddingTop="29px"   
  33.              android:background="@drawable/change" />   
  34.     </LinearLayout>   
  35.     <ImageView android:layout_width="wrap_content"   
  36.             android:layout_height="wrap_content"  android:src="@drawable/line" />   
  37.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
  38.         android:orientation="vertical" android:layout_width="80px" android:id="@+id/more"   
  39.         android:layout_height="wrap_content" android:background="@drawable/tab_one_normal">   
  40.         <TextView android:layout_width="wrap_content"   
  41.             android:layout_height="wrap_content" android:text="@string/more"   
  42.             android:gravity="center" android:paddingTop="29px"   
  43.              android:background="@drawable/more" />   
  44.     </LinearLayout>   
  45. </LinearLayout>   

 

代码详见:http://easymorse.googlecode.com/svn/trunk/android.bottom/

原文地址:https://www.cnblogs.com/stulife/p/1817363.html