TabLayout的使用

1、 添加design包

在main_activity.xml中添加如下代碼 :

< 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:layout_margin="5dp"
tools:context="com.usher.studytablayout.MainActivity">

<android.support.design.widget.TabLayout
    android:id="@+id/main_tablayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#D2A2CC" /></LinearLayout>

2、編寫MainActivity代碼

public class MainActivity extends AppCompatActivity {

private TabLayout tablayout;

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

    tablayout = (TabLayout) findViewById(R.id.main_tablayout);
    tablayout.addTab(tablayout.newTab().setText("新聞"));
    tablayout.addTab(tablayout.newTab().setText("天氣"));
    tablayout.addTab(tablayout.newTab().setText("娛樂"));
}

}

這個(gè)東西就是TabLayout的基本顯示,然后它有一些屬性如下:

  • tabIndicatorColor 下方滾動(dòng)的下劃線顏色
  • tabTextColor 默認(rèn)的文字顏色
  • tabSelectedTextColor 被選中后文字的顏色
  • tabIndicatorHeight 下劃線的高度
  • app:tabMode="scrollable" 很多標(biāo)題的時(shí)候可以左右滑動(dòng)
然后關(guān)于TabLayout的顯示你還可以給他加上圖片,代碼如下:

public class MainActivity extends AppCompatActivity {

private TabLayout tablayout;

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

    tablayout = (TabLayout) findViewById(R.id.main_tablayout);
    tablayout.addTab(tablayout.newTab().setText("新聞").setIcon(R.mipmap.icon1));
    tablayout.addTab(tablayout.newTab().setText("天氣").setIcon(R.mipmap.icon2));
    tablayout.addTab(tablayout.newTab().setText("娛樂").setIcon(R.mipmap.icon3 ));
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容