国内大部分应用使用底部导航栏,
底部导航栏 是国内 APP 常见的导航方式, 历经: TabActivity -> ActionBar -> TabHost -> FragmentTabHost -> TabLayout 等等方式。
安卓官方文档 https://developer.android.google.cn/develop/index.html
TabLayout
是安卓辅助设计类库的一部分,设计类库的用法可以参考泡在网上的日子。
TabLayout 用法:
-
使用代码添加 Item
TabLayout tabLayout = ...;
tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab 3")); -
使用布局文件添加 Item
<android.support.design.widget.TabLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"><android.support.design.widget.TabItem android:text="@string/tab_text"/> <android.support.design.widget.TabItem android:icon="@drawable/ic_android"/> </android.support.design.widget.TabLayout>
制作底部导航栏
The bottom navigation bar on mobileTabActivity
代码:
public class TabActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
}
}
activity_tab
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/holo_blue_bright" />
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="@android:color/white"
app:tabIndicatorColor="@android:color/transparent">
<android.support.design.widget.TabItem android:layout="@layout/tab_item" />
<android.support.design.widget.TabItem android:layout="@layout/tab_item" />
<android.support.design.widget.TabItem android:layout="@layout/tab_item" />
<android.support.design.widget.TabItem android:layout="@layout/tab_item" />
</android.support.design.widget.TabLayout>
</LinearLayout>
tab_item
代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:contentDescription="@string/app_name"
android:scaleType="fitXY"
android:src="@drawable/selector_tab_image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/android"
android:textColor="@color/selector_tab_text" />
</LinearLayout>
图片选择器 selector_tab_image
代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/ic_launcher_0" android:state_selected="true" />
<item android:drawable="@mipmap/ic_launcher_1" android:state_selected="false" />
</selector>
文字选择器 selector_tab_text
代码:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#F00" android:state_selected="true" />
<item android:color="#0FF" android:state_selected="false" />
</selector>
使用 Java 代码添加 Item:
public class TabActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.tab_item));
tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.tab_item));
tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.tab_item));
tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.tab_item));
}
}
循环添加
public class TabActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tab);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
String[] texts = {"安卓", "Android", "Java", "Kotlin"};
for (String s:texts) {
View itemView = LayoutInflater.from(this).inflate(R.layout.tab_item, tabLayout, false);
TextView txtView = (TextView) itemView.findViewById(R.id.tv_tab_text);
txtView.setText(s);
tabLayout.addTab(tabLayout.newTab().setCustomView(itemView));
}
}
}
...
-
app:tabIndicatorColor="@android:color/transparent"
隐藏TabLayout
指示器; -
app:tabBackground="@android:color/white"
取消导航栏被点击时的阴影效果。
网友评论