【问题标题】:How to change the selected tab indicator background image [closed]如何更改选定的选项卡指示器背景图像[关闭]
【发布时间】:2016-10-01 16:32:24
【问题描述】:

我正在使用 Tablayout,我想更改所选标签指示器的背景并设置他的宽度。我怎样才能在我的项目中实现这一点

我想像上图那样制作标签选择器

【问题讨论】:

  • 到目前为止你尝试了什么????????
  • 我想自定义指标并将图像作为指标。
  • 那个解决方案对我不起作用。
  • 我只想更改指示器而不是完整标签。(显示在标签底部)

标签: android


【解决方案1】:

要设置宽度,请使用以下代码:

public class CustomTabLayout extends TabLayout {
public CustomTabLayout(Context context) {
    super(context);
}

public CustomTabLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    try {
        if (getTabCount() == 0)
            return;
        Field field = TabLayout.class.getDeclaredField("mTabMinWidth");
        field.setAccessible(true);
        field.set(this, (int) (getMeasuredWidth() / (float) getTabCount()));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

}

用于更改背景:

res/layout/somefile.xml:

<android.support.design.widget.TabLayout
....
app:tabBackground="@drawable/tab_color_selector"
...
/>

然后在选择器res/drawable/tab_color_selector.xml:

 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/tab_background_selected" android:state_selected="true"/>
    <item android:drawable="@color/tab_background_unselected"/>
</selector>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多