2016-10-17 69 views
1

我加標籤動態如果標籤是4,則它是良好的顯示效果,但如果標籤是2或3那麼它就不是腳匹配母體。當標籤添加動態未設置寬度,以匹配父

代碼

public void setUpTab(TabLayout tabLayout, String count, String typeId) { 

    TabLayout.Tab tab = tabLayout.newTab(); 

    RelativeLayout relativeLayout = (RelativeLayout) 
      LayoutInflater.from(this).inflate(R.layout.custom_tab, tabLayout, false); 
    TextView tabTextView = (TextView) relativeLayout.findViewById(R.id.txt_tab); 
    tabTextView.setText(count); 
    tab.setTag(typeId); 
    tab.setCustomView(tabTextView); 
    tabLayout.addTab(tab); 
} 

XML代碼:

<android.support.design.widget.TabLayout 
    android:id="@+id/tabsRoleType" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="@dimen/com_facebook_likeboxcountview_border_radius" 
    app:tabBackground="@drawable/tab_rounded_background" 
    app:tabGravity="fill" 
    app:tabIndicatorHeight="0dp" 
    app:tabSelectedTextColor="@color/WhiteColor" 
    app:tabTextAppearance="@style/TextAppearance.Design.Tab" 
    app:tabTextColor="@color/BlackColor" /> 

enter image description here

回答

1

添加android:tabMaxWidthandroid:tabModeandroid:tabgravity

示例XML代碼:

<android.support.design.widget.TabLayout 
    android:id="@+id/tab" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/actionBarSize" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    app:tabIndicatorColor="@color/colorPrimary" 
    app:tabIndicatorHeight="3dp" 
    app:tabMaxWidth="0dp" 
    app:tabGravity="fill" 
    app:tabMode="fixed" 
    app:tabSelectedTextColor="@color/colorPrimary" 
    app:tabTextAppearance="@style/TabLayoutTextStyle" 
    app:tabTextColor="@color/textCol" /> 
+0

雅我正確的嘗試,但當選項卡是超過4它不可見在UI中是正確的 –

+0

看到這個鏈接可能會幫助你[鏈接]( http://stackoverflow.com/questions/31448504/how-to-scroll-tablayout-programatically-android) –

+0

柯Vartthan感謝 –

1

添加/更新遵循XML行TabLayout

<android.support.design.widget.TabLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:tabMaxWidth="0dp" 
     app:tabGravity="fill" 
     app:tabMode="fixed" /> 

良好做法是app:tabMode="scrollable"

+0

雅好吧,我嘗試,但是當標籤超過4是不可見的正確 –

相關問題