android底部导航栏之FragmentTabHost+FrameLayout

最近使用FragmentTabHost+FrameLayout做APP的底部导航栏,感觉非常的快速有效,做个笔记记录下:

1.主页面代码:

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.real_tab_content)
    FrameLayout realTabContent;
    @BindView(R.id.tab_content)
    FrameLayout tabContent;
    @BindView(R.id.tab_host)
    FragmentTabHost tabHost;

    //需要的资源信息
    private final String mTabSpec[]={"head","classify","heart","my"};
    //文字信息
    private final String mIndicator[]={"首页","分类","喜欢","我的"};
    //导航图信息
    private final int mTabImage[]={R.drawable.tab_home_item,R.drawable.tab_classify_item,R.drawable.tab_heart_item,R.drawable.tab_user_item};
    //导航fragment
    private final Class mFragmentClass[]={HomeFragment.class, ClassifyFragment.class, HeartFragment.class, MyFragment.class};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        //设置添加tab
        tabHost.setup(this,getSupportFragmentManager(),R.id.real_tab_content);
        for(int i=0;i<mTabSpec.length;i++){
            tabHost.addTab(tabHost.newTabSpec(mTabSpec[i]).setIndicator(getTabView(i)),mFragmentClass[i],null);
        }
    }

    //设定每一个的TabView
    private View getTabView(int index){
        View view=View.inflate(this,R.layout.tab_item,null);
        ImageView tabImage=view.findViewById(R.id.tab_image);
        TextView tabTitle=view.findViewById(R.id.tab_title);

        tabImage.setImageResource(mTabImage[index]);
        tabTitle.setText(mIndicator[index]);

        return view;
    }
}

2.主页面布局

<?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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.liveviewdemo.ui.activity.MainActivity">

    <FrameLayout
        android:id="@+id/real_tab_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>
    <View
        android:background="#333333"
        android:layout_width="match_parent"
        android:layout_height="1dp"></View>
    <android.support.v4.app.FragmentTabHost
        android:id="@+id/tab_host"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <FrameLayout
            android:id="@+id/tab_content"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0">
        </FrameLayout>
    </android.support.v4.app.FragmentTabHost>

</LinearLayout>

3.TabView布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/tab_image"
        android:paddingTop="2dp"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:contentDescription="@string/tab_image"/>
    <TextView
        android:id="@+id/tab_title"
        android:textSize="12sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

4.图片资源文件(ps:给出一个做为样式,代码都是一样的只是图片不同而已):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@mipmap/ic_home_press"/>
    <item android:state_selected="true" android:drawable="@mipmap/ic_home_press"/>
    <item android:drawable="@mipmap/ic_home"/>
</selector>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现 Android 底部导航栏可以使用以下步骤: 1. 在布局文件中添加 BottomNavigationView 控件。 2. 在 Java 文件中获取 BottomNavigationView 的实例,并设置导航栏的选项。 3. 创建多个 Fragment 来作为导航栏的选项内容。 4. 在 Java 文件中设置导航栏选项的监听器,根据选项的不同切换 Fragment。 以下是一个基本的 Android 底部导航栏实现示例代码: XML 布局文件: ``` <RelativeLayout <!-- other views --> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottom_navigation_view" /> <android.support.design.widget.BottomNavigationView android:id="@+id/bottom_navigation_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" app:menu="@menu/bottom_navigation_menu" /> </RelativeLayout> ``` Java 文件: ``` public class MainActivity extends AppCompatActivity { private BottomNavigationView bottomNavigationView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bottomNavigationView = findViewById(R.id.bottom_navigation_view); bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.navigation_home: switchFragment(new HomeFragment()); return true; case R.id.navigation_search: switchFragment(new SearchFragment()); return true; case R.id.navigation_notifications: switchFragment(new NotificationFragment()); return true; } return false; } }); // 默认显示 HomeFragment switchFragment(new HomeFragment()); } private void switchFragment(Fragment fragment) { getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit(); } } ``` 其中,我们使用了 FrameLayout 来作为 Fragment 的容器,将 Fragment 切换时使用了 FragmentTransaction。同时,我们使用了 BottomNavigationView 的 setOnNavigationItemSelectedListener 来监听导航栏选项点击事件,并根据不同选项切换不同的 Fragment
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值