【问题标题】:When tab add dynamic it is not set width to match parentWhen tab add dynamic it is not set width to match parent
【发布时间】:2016-10-17 14:17:58
【问题描述】:

如果 tab 为 4,我动态添加 tab,那么它显示得很好,但如果 tab 是 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" />

【问题讨论】:

    标签: android android-layout


    【解决方案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" />
    

    【讨论】:

    • ya对我试试,但是当标签超过4时,在ui span>中不可见。
    • 看到这个链接可能对你有帮助link
    • Ko Vartthan 谢谢
    • 我添加了选项卡的固定模式,然后选项卡文本在第二行向下移动
    【解决方案2】:

    在 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"

    【讨论】:

    • ya对我试试,但是当标签超过4时,它不可见 span>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    • 2011-12-24
    • 2022-12-02
    • 2022-12-02
    • 2022-12-02
    相关资源
    最近更新 更多