Android Studio 底部导航栏

TabActivity

因为不想用fragment,所以导航栏仅仅涉及activity的跳转(其实是因为先做完了几个Activity然后想做导航栏的。)


public class BottomTabActivity extends TabActivity1 implements OnClickListener{
    /**
     * 显示的三个Activity的class
     */
    private Class<?> activities[] = {Activity1.class, Activity2.class, Activity3.class};
    /**i
     * 菜单的三个button
     */
    private RelativeLayout[] btn = new RelativeLayout[3];
    /**
     * 当前的选择
     */
    private int currentSelect;

    private TextView btn_1_text;
    private TextView btn_2_text;
    private TextView btn_3_text;
    private ImageView btn_1;
    private ImageView btn_2;
    private ImageView btn_3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottomtab);
        setContainer(R.id.fl_container);
        btn_trans =(ImageView)findViewById(R.id.btn_1);
        btn_yizu =(ImageView)findViewById(R.id.btn_2);
        btn_mine =(ImageView)findViewById(R.id.btn_3);
        btn_trans_text = (TextView)findViewById(R.id.btn_1_text);
        btn_yizu_text = (TextView)findViewById(R.id.btn_2_text);
        btn_mine_text = (TextView)findViewById(R.id.btn_3_text);
        btn[0] = (RelativeLayout) findViewById(R.id.bottom_bar_1_btn);
        btn[0].setOnClickListener(this);
        btn[1] = (RelativeLayout) findViewById(R.id.bottom_bar_2_btn);
        btn[1].setOnClickListener(this);
        btn[2] = (RelativeLayout) findViewById(R.id.bottom_bar_3_btn);
        btn[2].setOnClickListener(this);
        showActivity(activities[0]);
        btn_trans.setImageResource(R.drawable.icon_trans2);
        btn_trans_text.setTextColor(Color.parseColor("#2E59F9"));
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.bottom_bar_1_btn:
                btn_trans.setImageResource(R.drawable.icon_trans2);
                btn_trans_text.setTextColor(Color.parseColor("#2E59F9"));

                btn_yizu.setImageResource(R.drawable.icon_minzu1);
                btn_yizu_text.setTextColor(Color.parseColor("#48000000"));

                btn_mine.setImageResource(R.drawable.icon_me1);
                btn_mine_text.setTextColor(Color.parseColor("#48000000"));
                currentSelect = 0;
                showActivity(activities[0]);
                break;
            case R.id.bottom_bar_2_btn:
                btn_trans.setImageResource(R.drawable.icon_trans1);
                btn_trans_text.setTextColor(Color.parseColor("#48000000"));
                btn_yizu.setImageResource(R.drawable.icon_minzu2);
                btn_yizu_text.setTextColor(Color.parseColor("#2E59F9"));
                btn_mine.setImageResource(R.drawable.icon_me1);
                btn_mine_text.setTextColor(Color.parseColor("#48000000"));
                currentSelect = 1;
                showActivity(activities[1]);
                break;
            case R.id.bottom_bar_3_btn:
                btn_trans.setImageResource(R.drawable.icon1);
                btn_trans_text.setTextColor(Color.parseColor("#48000000"));
                btn_yizu.setImageResource(R.drawable.icon2);
                btn_yizu_text.setTextColor(Color.parseColor("#48000000"));
                btn_mine.setImageResource(R.drawable.icon3);
                btn_mine_text.setTextColor(Color.parseColor("#2E59F9"));
                currentSelect = 2;
                showActivity(activities[2]);
                break;
        }
    }
}

TabActivity1

public class TabActivity1 extends ActivityGroup {
    private ViewGroup container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    /**
     * 通过id设置Activity显示的container,该container必须是继承ViewGroup的
     */
    protected void setContainer(int resId) {
        container = (ViewGroup) findViewById(resId);
    }

    /**
     * 通过Activity的class显示Activity
     */
    protected void showActivity(Class<?> activityClass) {

        Intent intent = new Intent(this,  activityClass);
        // 检查container是否有显示的Activity。假设有,先移除
        View activity = container.getChildAt(0);
        if (activity != null) {
            // 移除显示的activity的View
            container.removeAllViews();
            // 通过ActivityManager移除activity
            getLocalActivityManager().removeAllActivities();
        }
        // 启动新的activity。并将该activity的根视图加入到contanier中
        container.addView(getLocalActivityManager().startActivity(activityClass.getName(), intent).getDecorView());
    }
}

activity_tab.xml

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".BottomTabActivity">

     <FrameLayout
         android:id="@+id/fl_container"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_above="@+id/bottom_bar"/>

     <LinearLayout
         android:id="@+id/bottom_bar"
         android:layout_alignParentBottom="true"
         android:orientation="horizontal"
         android:background="#F2F2F8"
         android:layout_width="match_parent"
         android:layout_height="52dp">
         <RelativeLayout
             android:id="@+id/bottom_bar_1_btn"
             android:layout_weight="1"
             android:layout_width="0dp"
             android:layout_height="match_parent">
             <TextView
                 android:id="@+id/btn_trans_text"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_alignParentBottom="true"
                 android:layout_centerHorizontal="true"
                 android:layout_marginBottom="2dp"
                 android:gravity="center"
                 android:text="1"
                 android:textColor="#48000000"
                 android:textSize="13sp"/>
             <ImageView
                 android:id="@+id/btn_trans"
                 android:layout_width="30dp"
                 android:layout_height="30dp"
                 android:layout_above="@+id/btn_trans_text"
                 android:layout_alignParentTop="true"
                 android:layout_centerHorizontal="true"
                 android:layout_marginTop="2dp"
                 android:src="@drawable/icon1"/>
         </RelativeLayout>

         <RelativeLayout
             android:id="@+id/bottom_bar_2_btn"
             android:layout_weight="1"
             android:layout_width="0dp"
             android:layout_height="match_parent">
             <TextView
                 android:id="@+id/btn_yizu_text"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_alignParentBottom="true"
                 android:layout_centerHorizontal="true"
                 android:layout_marginBottom="2dp"
                 android:gravity="center"
                 android:text="2"
                 android:textColor="#48000000"
                 android:textSize="13sp"/>
             <ImageView
                 android:id="@+id/btn_yizu"
                 android:layout_width="33dp"
                 android:layout_height="33dp"
                 android:layout_above="@+id/btn_yizu_text"
                 android:layout_alignParentTop="true"
                 android:layout_centerHorizontal="true"
                 android:layout_marginTop="2dp"
                 android:src="@drawable/icon2"/>
         </RelativeLayout>
         <RelativeLayout
             android:id="@+id/bottom_bar_3_btn"
             android:layout_weight="1"
             android:layout_width="0dp"
             android:layout_height="match_parent">
             <TextView
                 android:id="@+id/btn_mine_text"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_alignParentBottom="true"
                 android:layout_centerHorizontal="true"
                 android:layout_marginBottom="2dp"
                 android:gravity="center"
                 android:text="我的"
                 android:textColor="#48000000"
                 android:textSize="13sp"/>
             <ImageView
                 android:id="@+id/btn_mine"
                 android:layout_width="25dp"
                 android:layout_height="25dp"
                 android:layout_above="@+id/btn_mine_text"
                 android:layout_alignParentTop="true"
                 android:layout_centerHorizontal="true"
                 android:layout_marginTop="2dp"
                 android:src="@drawable/icon3"/>
         </RelativeLayout>
     </LinearLayout>

</RelativeLayout>
  • 2
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值