Android Material Design TabLayout属性app:tabMode和app: tabGravity

简介: Android Material Design TabLayout属性app:tabMode和app: tabGravityAndroid Material Design 中的TabLayout有两个比较有用的属性 app:tabMode、app:tabGravity,(1)app:tabMode有两个值:fixed和scrollable。

Android Material Design TabLayout属性app:tabMode和app: tabGravity

Android Material Design 中的TabLayout有两个比较有用的属性 app:tabMode、app:tabGravity,
(1)app:tabMode有两个值:fixed和scrollable。
(2)app:tabGravity有两个值:fill和center。
比较常用的是app:tabMode设置值scrollable,以及app:tabGravity设置值center。
比如,当app:tabMode设置值scrollable表示此TabLayout中当子view超出屏幕边界时候,将提供滑动以便滑出不可见的那些子view。
而app:tabGravity设置值center,在有些情况下,比如TabLayout中子view较少需要居中显示时候的情景。
现在给出一个例子加以说明。
测试的MainActivity.java:

package zhangphil.view;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;

public class MainActivity extends Activity {

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

		TabLayout tabLayout1 = (TabLayout) findViewById(R.id.tabLayout1);
		TabLayout tabLayout2 = (TabLayout) findViewById(R.id.tabLayout2);
		TabLayout tabLayout3 = (TabLayout) findViewById(R.id.tabLayout3);
		TabLayout tabLayout4 = (TabLayout) findViewById(R.id.tabLayout4);

		for (int i = 0; i < 6; i++) {
			tabLayout1.addTab(tabLayout1.newTab().setText("卡" + i));
			tabLayout2.addTab(tabLayout2.newTab().setText("卡" + i));
		}

		for (int i = 0; i < 10; i++) {
			tabLayout3.addTab(tabLayout3.newTab().setText("卡" + i));
			tabLayout4.addTab(tabLayout4.newTab().setText("卡" + i));
		}

	}
}

MainActivity.java需要的布局文件activity_main.xml文件:

<?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="wrap_content"
    android:orientation="vertical" >

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="fixed" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="center"
        app:tabMode="fixed" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="fill"
        app:tabMode="scrollable" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabLayout4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabGravity="center"
        app:tabMode="scrollable" />

</LinearLayout>


效果如图所示:

相关文章
|
6天前
|
存储 Java API
Android 浅度解析:mk预置AAR、SO文件、APP包和签名
Android 浅度解析:mk预置AAR、SO文件、APP包和签名
96 0
|
5天前
|
Android开发 移动开发 小程序
binder机制原理面试,安卓app开发教程
binder机制原理面试,安卓app开发教程
binder机制原理面试,安卓app开发教程
|
6天前
|
Java 开发工具 Android开发
Android mk 集成app
Android mk 集成app
15 5
|
6天前
|
移动开发 jenkins 持续交付
jenkins编译H5做的android端编译卫士app记录
jenkins编译H5做的android端编译卫士app记录
|
6天前
|
Android开发
Android APP 隐藏系统软键盘的方法
Android APP 隐藏系统软键盘的方法
38 0
|
6天前
|
Android开发
Android修改默认system/bin/下可执行程序拥有者和权限,使用实例,只有root和系统app权限才能执行某个命令。
Android修改默认system/bin/下可执行程序拥有者和权限,使用实例,只有root和系统app权限才能执行某个命令。 【5月更文挑战第2天】
21 0
|
6天前
|
XML Java Android开发
Android系统 添加动态控制屏幕方向、强制APP横竖屏方向
Android系统 添加动态控制屏幕方向、强制APP横竖屏方向
40 1
|
6天前
|
测试技术 Android开发
Android App获取不到pkgInfo信息问题原因
Android App获取不到pkgInfo信息问题原因
23 0
|
6天前
|
Android开发 UED 开发者
解释Android App Bundle是什么,它的优势是什么?
Android App Bundle是Google开发的优化应用分发技术,它打包应用及资源以减少下载大小,加快加载速度,节省用户流量。App Bundle支持离线使用,简化更新过程,提升用户体验。开发人员借此能更高效地构建和分发Android应用。
13 0
|
6天前
|
Android开发
安卓远离手机app
远离手机是专门为防止年轻人上瘾而打造的生活管理类的软件,适度用手机,保护眼睛,节约时间。
12 0
安卓远离手机app