TabHost、RadioGroup、ToolBar实现底部菜单导航

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.tianshuai.mainframedemo.MainActivity">

    <include android:id="@+id/id_toolbar"
        layout="@layout/tool_bar_layout"/>
    <include android:id="@+id/tabhost"
        layout="@layout/tabhost"/>

</LinearLayout>

Toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/id_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:minHeight="?attr/actionBarSize"

    android:background="#00000000"/>


tabhost.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFFFF"
        android:orientation="vertical" >

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="fill_parent" >

                <fragment
                    android:id="@+id/fragment_main"
                    android:name="com.example.tianshuai.mainframedemo.fragment.MainFragment"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />

                <fragment
                    android:id="@+id/fragment_mycenter"
                    android:name="com.example.tianshuai.mainframedemo.fragment.MyCenterFragment"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />

                <fragment
                    android:id="@+id/fragment_search"
                    android:name="com.example.tianshuai.mainframedemo.fragment.SearchFragment"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />
            </FrameLayout>
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.0"
            android:visibility="gone" />

             <!-- 我只是一条线 -->
        <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="3dp"
        android:background="#3A000000" >
        </LinearLayout>

        <RadioGroup
            android:id="@+id/radiogroup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:paddingTop="3dp"
            android:paddingBottom="3dp"
            android:background="@drawable/radiogroup_shape"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <!-- android:background="@drawable/bk_footer" -->

            <RadioButton
                android:id="@+id/radio_search"
                style="@style/main_tab_bottom"
                android:layout_weight="1"
                android:text="搜索"
                android:drawableTop="@drawable/footer_search_selector"
                 />

            <RadioButton
                android:id="@+id/radio_main"
                style="@style/main_tab_bottom"
                android:layout_weight="1"
                android:button="@null"
                android:checked="true"
                android:drawableTop="@drawable/footer_main_selector"
                android:text="首 页" />

            <RadioButton
                android:id="@+id/radio_mycenter"
                style="@style/main_tab_bottom"
                android:layout_weight="1"
                android:drawableTop="@drawable/footer_mycenter_selector"
                android:text="个人中心" />
        </RadioGroup>
    </LinearLayout>

</TabHost>


MainActivity

public class MainActivity extends AppCompatActivity {
    private TabHost tabHost;
    private RadioGroup radiogroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initToolbar();
        initView();

    }
    public void initToolbar(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.id_toolbar);
        // App Logo
      //  toolbar.setLogo(R.mipmap.chat_send_normal);
        // Title
        toolbar.setTitle("DEMO");
        toolbar.setTitleTextColor(getResources().getColor(R.color.colorPrimary));
        // Sub Title
//        toolbar.setSubtitle("Sub title");
        setSupportActionBar(toolbar);
        //Navigation Icon
        toolbar.setNavigationIcon(R.mipmap.profile_card_company_icon);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
    }

    public void initView(){
        radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
        tabHost = (TabHost) findViewById(R.id.tabhost);
        tabHost.addTab(tabHost.newTabSpec("main").setIndicator("main")
                .setContent(R.id.fragment_main));
        tabHost.addTab(tabHost.newTabSpec("mycenter").setIndicator("mycenter")
                .setContent(R.id.fragment_mycenter));
        tabHost.addTab(tabHost.newTabSpec("search").setIndicator("search")
                .setContent(R.id.fragment_search));
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                int currentTab = tabHost.getCurrentTab();
                switch (checkedId) {
                    case R.id.radio_main:
                        tabHost.setCurrentTabByTag("main");

                        //如果需要动画效果就使用
                        //setCurrentTabWithAnim(currentTab, 0, "main");
                        getSupportActionBar().setTitle("首页");
                        break;
                    case R.id.radio_mycenter:
                        //tabHost.setCurrentTabByTag("mycenter");
                        //setCurrentTabWithAnim(currentTab, 1, "mycenter");
                        getSupportActionBar().setTitle("个人中心");

                        break;
                    case R.id.radio_search:
                        tabHost.setCurrentTabByTag("search");
                        getSupportActionBar().setTitle("搜索");
                        break;
                }

            }
        });


    }


    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            String msg = "";
            switch (menuItem.getItemId()) {
                case R.id.action_search:
                    msg += "Click search";
                    break;
                case R.id.action_add:
                    msg += "Click add";
                    break;
            }

            if(!msg.equals("")) {
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    };

    @Override
   public boolean onCreateOptionsMenu(Menu menu) {
        /**
         * 此方法用于初始化菜单,其中menu参数就是即将要显示的Menu实例。 返回true则显示该menu,false 则不显示;
         * (只会在第一次初始化菜单时调用) Inflate the menu; this adds items to the action bar
        * if it is present.
         */
       getMenuInflater().inflate(R.menu.menu_main, menu);
       return true;
    }
}

radioButton的样式

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@mipmap/chat_profile_pressed" android:state_pressed="true"/>
    <item android:drawable="@mipmap/chat_profile_pressed" android:state_checked="true"/>
    <item android:drawable="@mipmap/chat_profile_normal"/>
</selector>

   <style name="main_tab_bottom">
        <item name="android:textSize">10sp</item>
        <item name="android:textColor">#ff000000</item>
        <item name="android:ellipsize">marquee</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:background">#00000000</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:button">@null</item>
        <item name="android:singleLine">true</item>
        <item name="android:drawablePadding">2dp</item>
        <item name="android:layout_weight">1.0</item>


MainFragment

public class MainFragment extends Fragment {
    private String context;
    private TextView mTextView;

    public  MainFragment(){
        super();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main,container,false);
        return view;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值