Android Material Design-TabLayout的使用


TabLayout 位於 android.support.design.widget.TabLayout。

一般與 ViewPager 結合在一起使用。以前有開源庫 viewpagerindicator 也可以實現,不過 TabLayout 是官方提供的。

以下使用 ViewPager + TabLayout 實現點擊 tab 切換頁面的效果。其中 ViewPager 中使用的是 TextView 來顯示一個詞,可以把 TextView 更換為 Fragment 等實現更加復雜的內容。

布局文件 activity_main.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout
 3     xmlns:android="http://schemas.android.com/apk/res/android"
 4     xmlns:app="http://schemas.android.com/apk/res-auto"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent">
 7   <android.support.v7.widget.Toolbar
 8       android:id="@+id/toolbar"
 9       android:layout_width="match_parent"
10       android:layout_height="wrap_content"
11       android:background="@color/colorPrimary"
12       android:minHeight="?attr/actionBarSize"
13       android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
14       app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
15   <android.support.design.widget.TabLayout
16       android:id="@+id/tablayout"
17       android:layout_width="match_parent"
18       android:layout_height="wrap_content"
19       android:layout_below="@+id/toolbar"/>
20   <android.support.v4.view.ViewPager
21       android:id="@+id/viewpager"
22       android:layout_width="match_parent"
23       android:layout_height="match_parent"
24       android:layout_below="@+id/tablayout"/>
25 </RelativeLayout>

 

ViewPagerAdapter.java

 1 import android.content.Context;
 2 import android.graphics.Color;
 3 import android.support.v4.view.PagerAdapter;
 4 import android.view.Gravity;
 5 import android.view.View;
 6 import android.view.ViewGroup;
 7 import android.widget.TextView;
 8 
 9 public class ViewPagerAdapter extends PagerAdapter {
10   private static final String TAG = "ViewPagerAdapter";
11   private String[] tabTitles = { "全部", "視頻", "聲音", "圖片", "段子", "廣告", "劇情" };
12   private Context mContext;
13 
14   public ViewPagerAdapter(Context context) {
15     mContext = context;
16   }
17 
18   @Override public int getCount() {
19     return tabTitles.length;
20   }
21 
22   @Override public boolean isViewFromObject(View view, Object object) {
23     return view == object;
24   }
25 
26   @Override public Object instantiateItem(ViewGroup container, int position) {
27     TextView view = new TextView(mContext);
28     view.setText(tabTitles[position]);
29     view.setTextColor(Color.BLACK);
30     view.setTextSize(20);
31     view.setGravity(Gravity.CENTER);
32     container.addView(view);
33     return view;
34   }
35 
36   @Override public void destroyItem(ViewGroup container, int position, Object object) {
37     container.removeView((TextView) object);
38   }
39 
40   @Override public CharSequence getPageTitle(int position) {
41     return tabTitles[position];
42   }
43 }

 

MainActivity.java

 1 public class MainActivity extends AppCompatActivity {
 2 
 3   @Override protected void onCreate(Bundle savedInstanceState) {
 4     super.onCreate(savedInstanceState);
 5     setContentView(R.layout.activity_main);
 6     TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
 7     ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
 8     ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this);
 9     viewPager.setAdapter(adapter);
10     tabLayout.setupWithViewPager(viewPager);
11   }
12 }

實現效果圖:

 
        

TabLayout 樣式
app:tabSelectedTextColor:Tab被選中字體的顏色
app:tabTextColor:Tab未被選中字體的顏色
app:tabIndicatorColor:Tab指示器下標的顏色

app:tabIndicatorHeight="2dp"
app:tabPaddingStart="12dp"
app:tabPaddingEnd="12dp"
app:tabPaddingTop="12dp"
app:tabPaddingBottom="12dp"
app:tabPadding="12dp"
app:tabBackground="@android:color/darker_gray"
app:tabTextAppearance=""

app:tabMode:可選scrollable和fixed,默認為fixed。scrollable 適合多個 tab 的情況

當 tab 的數量不足以鋪滿全屏的時候,且 tabMode 為 scrollable 的時候,會出現以下情況:只需要把 tabMode 設置為 fixed 即可或者去掉這個屬性,因為默認為 fixed。

參考:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0731/3247.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM