Android 自定义底部导航栏&消息显示

整体布局是一个线性布局里面包括两个线性布局,上面的线性布局可以用其他布局替换,这个看个人爱好了;

下面线性布局是四个等分的相对布局,进货单有一个消息提示,所以用相对布局好控制位置;

先看下进货单的布局:

 

<RelativeLayout
        android:id="@+id/shopping_cart_page"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginTop="4dp"
        android:layout_weight="1">

        <RadioButton
            android:id="@+id/shopping_cart_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background="@null"
            android:button="@null"
            android:clickable="false"
            android:drawablePadding="5dp"
            android:drawableTop="@drawable/selector_tab_stock_list"
            android:gravity="center"
            android:text="进货单"
            android:textColor="@drawable/tab_text_selector"
            android:textSize="10sp" />

        <TextView
            android:id="@+id/shopping_cart_num"
            android:layout_width="16dp"
            android:layout_height="16dp"
            android:layout_alignRight="@id/shopping_cart_button"
            android:layout_alignTop="@id/shopping_cart_button"
            android:layout_marginTop="-6dp"
            android:layout_gravity="right"
            android:background="@drawable/msg_num_shape"
            android:clickable="false"
            android:gravity="center"
            android:text="99+"
            android:textColor="@color/white"
            android:textSize="8sp"
            />

    </RelativeLayout>


整体布局就不再复制了,因为其他三个和这个一样,只是没有TextView

 

 

MainActivity代码:

 

public class MainActivity extends FragmentActivity implements View.OnClickListener {

    private RadioButton btnHome,btnGoods,btnShopCart,btnUserVip;
    private RelativeLayout homePage,goodsPage,shoppingPage,usePage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container,new HomeFragment())
                .commit();
    }

    @Override
    public void onClick(View v) {

        BaseFragment fragment=null;

        switch (v.getId()){

            case R.id.homepage:
                fragment=new HomeFragment();
                changeState(true,false,false,false);
                break;

            case R.id.goods_page:
                fragment=new GoodsFragment();
                changeState(false,true,false,false);
                break;

            case R.id.shopping_cart_page:
                fragment=new ShoppingFragment();
                changeState(false,false,true,false);
                break;

            case R.id.user_page:
                fragment=new UserFragment();
                changeState(false,false,false,true);
                break;

        }

        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container,fragment)
                .commit();

    }

    private void changeState(boolean homeB,boolean goodsB,boolean shopB,boolean userB ) {
        btnHome.setChecked(homeB);
        btnGoods.setChecked(goodsB);
        btnShopCart.setChecked(shopB);
        btnUserVip.setChecked(userB);
    }

    private void initView() {
        homePage= (RelativeLayout) findViewById(R.id.homepage);
        goodsPage= (RelativeLayout) findViewById(R.id.goods_page);
        shoppingPage= (RelativeLayout) findViewById(R.id.shopping_cart_page);
        usePage= (RelativeLayout) findViewById(R.id.user_page);
        btnHome= (RadioButton) findViewById(R.id.home_button);
        btnGoods= (RadioButton) findViewById(R.id.goods_button);
        btnShopCart= (RadioButton) findViewById(R.id.shopping_cart_button);
        btnUserVip= (RadioButton) findViewById(R.id.user_button);
        homePage.setOnClickListener(this);
        goodsPage.setOnClickListener(this);
        shoppingPage.setOnClickListener(this);
        usePage.setOnClickListener(this);
    }
}

非常简单的实现方式就能实现,当然大家可以进一步的封装;

 

目前公司正在做的项目就是使用的这个框架;

 

大家可以根据需求选择,如果没有消息提示可以直接只用这个框架:http://blog.csdn.net/zheng_jiao/article/details/51746915

 

点击打开链接免费下载源码
 

  

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值