TabLayout標題文字不顯示的解決操作
問題描述:
使用Design包的TabLayout實現類似網易選項卡動態滑動效果的時候,使用addTab()方法給TabLayout動態新增標題的時候,標題可能會出現不顯示文字的情況。
分析:
真實情況並不是不顯示文字,二而是ViewPager又給TabLayout添加了許多的標題,導致之前手動新增的標題又被擠到了後面。不信你多往後翻一翻就出來了。
解決辦法:
不要為ViewPager手動使用addTab()方法新增標題,而應該先建立一個List集合,將其設定在PagerAdapter的getPageTitle方法中,程式碼如下:
@Override public CharSequence getPageTitle(int position) { return mList_title.get(position); }
補充知識:Android中 TabLayout的TabItem文字和圖片莫名不顯示問題處理
Tablayout在沒有設定介面卡的時候,TabItem的文字和icon是正常顯示的,可一旦設定上介面卡文字和icon就直接消失,這個時候有兩個解決辦法(本人暫時只有兩個)
1.效果圖:
2. 正常寫法設定介面卡後的實圖:
<android.support.design.widget.TabLayout android:id="@+id/fh_tab" app:tabIndicatorColor="@android:color/white" android:layout_marginTop="20dp" android:layout_below="@id/fh_title" android:layout_alignBottom="@+id/fh_iv" app:tabIndicatorHeight="4dp" app:tabTextColor="@color/tab_un_select" app:tabSelectedTextColor="@android:color/white" app:tabIndicatorFullWidth="false" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.TabItem android:text="熱點" android:icon="@drawable/ic_fire_gray" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <android.support.design.widget.TabItem android:text="要聞" android:icon="@drawable/ic_news_state" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <android.support.design.widget.TabItem android:text="時政" android:icon="@drawable/ic_politics_state" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <android.support.design.widget.TabItem android:text="熱點" android:icon="@drawable/ic_fire_gray" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <android.support.design.widget.TabItem android:text="粵頭條" android:icon="@drawable/ic_first_state" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <android.support.design.widget.TabItem android:text="分類" android:icon="@drawable/ic_classify_state" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.design.widget.TabLayout>
解決方法一:自己定TabItem的樣式(比較萬能,但是指示器的長度不太 “聽話”,就是不受app:tabIndicatorFullWidth = "false"的控制,(自己去設定指示器的長度也沒用,想試請看最後的"設定指示器長度程式碼")) :
1.建立一個item_home_tab:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv" android:src="@drawable/ic_relative_true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_marginTop="3dp" android:textColor="@color/font2" android:id="@+id/tv" android:textSize="13sp" android:text="title" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
2.java中:
protected void init(){ fhPager.setAdapter(adapter); fhPager.setCurrentItem(0); fhPager.setOffscreenPageLimit(1); fhTab.setSelectedTabIndicator(R.drawable.bg_submit); fhTab.setupWithViewPager(fhPager); Objects.requireNonNull(fhTab.getTabAt(0)).setCustomView(getview("熱點",R.drawable.ic_fire_state)); Objects.requireNonNull(fhTab.getTabAt(1)).setCustomView(getview("要聞",R.drawable.ic_news_state)); Objects.requireNonNull(fhTab.getTabAt(2)).setCustomView(getview("時政",R.drawable.ic_politics_state)); Objects.requireNonNull(fhTab.getTabAt(3)).setCustomView(getview("粵頭條",R.drawable.ic_fire_state)); Objects.requireNonNull(fhTab.getTabAt(4)).setCustomView(getview("分類",R.drawable.ic_classify_state)); } private View getview(String title,int icon) { View view = LayoutInflater.from(getActivity()).inflate(R.layout.item_home_tab,null); TextView tv = view.findViewById(R.id.tv); ImageView iv = view.findViewById(R.id.iv); tv.setText(title); iv.setImageResource(icon); return view; }
結果1:當Tablayout 的app:tabIndicatorFullWidth = "false"時出來的效果圖:
結果2:當Tablayout 的app:tabIndicatorFullWidth = "true"出來的效果圖:
解決辦法二(指示器長度"聽話"(受app:tabIndicatorFullWidth = "false"的控制),但是icon的大小就只能由系統來定):
fhPager.setAdapter(adapter); fhPager.setCurrentItem(0); fhPager.setOffscreenPageLimit(1); fhTab.setSelectedTabIndicator(R.drawable.bg_submit); fhTab.setupWithViewPager(fhPager); //設定tabItem的文字和icon fhTab.getTabAt(0).setText("熱點").setIcon(R.drawable.ic_fire_state); fhTab.getTabAt(1).setText("要聞").setIcon(R.drawable.ic_news_state); fhTab.getTabAt(2).setText("時政").setIcon(R.drawable.ic_politics_state); fhTab.getTabAt(3).setText("粵頭條").setIcon(R.drawable.ic_first_state); fhTab.getTabAt(4).setText("分類").setIcon(R.drawable.ic_classify_state);
結果1:當Tablayout 的app:tabIndicatorFullWidth = "false"時出來的效果圖:
結果2:當Tablayout 的app:tabIndicatorFullWidth = "true"出來的效果圖:
end...
設定TabLayout指示器的長度:
//指示器長度 public static void setIndicator(TabLayout tabs,int leftDip,int rightDip,int bottomDip) { if (tabs == null) { return; } Class<? extends TabLayout> tabLayout = tabs.getClass(); Field tabStrip = null; try { tabStrip = tabLayout.getDeclaredField("mTabStrip"); } catch (NoSuchFieldException e) { e.printStackTrace(); return; } tabStrip.setAccessible(true); LinearLayout llTab = null; try { llTab = (LinearLayout) tabStrip.get(tabs); } catch (IllegalAccessException e) { e.printStackTrace(); return; } int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,leftDip,Resources.getSystem().getDisplayMetrics()); int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,rightDip,Resources.getSystem().getDisplayMetrics()); int bottom = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,bottomDip,Resources.getSystem().getDisplayMetrics()); for (int i = 0; i < llTab.getChildCount(); i++) { View child = llTab.getChildAt(i); child.setPadding(0,0); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,1); params.leftMargin = left; params.rightMargin = right; params.bottomMargin = bottom; child.setLayoutParams(params); child.invalidate(); } }
以上這篇TabLayout標題文字不顯示的解決操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。