android实现底部菜单栏

本文介绍了一种自定义视图实现底部菜单栏的方法,包括布局配置和代码实现,适用于Android程序开发。

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

image

实现的主要代码:

@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 version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
    android:orientation=”horizontal” android:layout_width=”fill_parent”
    android:layout_height=”wrap_content”>
    <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
        android:orientation=”vertical” android:layout_width=”80px”
        android:layout_height=”wrap_content” android:id=”@+id/home”
        android:background=”@drawable/tab_one_normal”>
        <TextView android:layout_width=”wrap_content”
            android:layout_height=”wrap_content” android:text=”@string/home”
            android:gravity=”center” android:paddingTop=”29px”
            android:background=”@drawable/home” />
    </LinearLayout>
    <ImageView android:layout_width=”wrap_content”
            android:layout_height=”wrap_content” android:src=”@drawable/line” />
    <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
        android:orientation=”vertical” android:layout_width=”80px”
        android:layout_height=”wrap_content” android:background=”@drawable/tab_one_normal”
        android:id=”@+id/publish”>
        <TextView android:layout_width=”wrap_content”
            android:layout_height=”wrap_content” android:text=”@string/publish”
            android:gravity=”center” android:paddingTop=”29px”
             android:background=”@drawable/publish” />
    </LinearLayout>
    <ImageView android:layout_width=”wrap_content”
            android:layout_height=”wrap_content” android:src=”@drawable/line” />
    <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
        android:orientation=”vertical” android:layout_width=”80px” android:id=”@+id/change”
        android:layout_height=”wrap_content” android:background=”@drawable/tab_one_normal”>
        <TextView android:layout_width=”wrap_content”
            android:layout_height=”wrap_content” android:text=”@string/change”
            android:gravity=”center” android:paddingTop=”29px”
             android:background=”@drawable/change” />
    </LinearLayout>
    <ImageView android:layout_width=”wrap_content”
            android:layout_height=”wrap_content” android:src=”@drawable/line” />
    <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
        android:orientation=”vertical” android:layout_width=”80px” android:id=”@+id/more”
        android:layout_height=”wrap_content” android:background=”@drawable/tab_one_normal”>
        <TextView android:layout_width=”wrap_content”
            android:layout_height=”wrap_content” android:text=”@string/more”
            android:gravity=”center” android:paddingTop=”29px”
             android:background=”@drawable/more” />
    </LinearLayout>
</LinearLayout>
源代码见:http://easymorse.googlecode.com/svn/trunk/android.bottom

转载于:https://my.oschina.net/xiahuawuyu/blog/82934

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值