Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在tablayout的非活动选项卡上设置具有特定颜色的下划线?我的自定义视图占用最大选项卡宽度。_Android_View_Android Custom View_Android Tablayout - Fatal编程技术网

Android 如何在tablayout的非活动选项卡上设置具有特定颜色的下划线?我的自定义视图占用最大选项卡宽度。

Android 如何在tablayout的非活动选项卡上设置具有特定颜色的下划线?我的自定义视图占用最大选项卡宽度。,android,view,android-custom-view,android-tablayout,Android,View,Android Custom View,Android Tablayout,我无法在TabLayout选项卡上设置自定义视图。我想在每个非活动选项卡上设置带有特定颜色的下划线(如活动选项卡上的指示器)。我是这样创建视图的: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_

我无法在TabLayout选项卡上设置自定义视图。我想在每个非活动选项卡上设置带有特定颜色的下划线(如活动选项卡上的指示器)。我是这样创建视图的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/colorPrimary">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="@style/tab_layout_item"
    android:layout_centerInParent="true"
    tools:text="All in"/>

<View
    android:layout_width="wrap_content"
    android:layout_height="7dp"
    android:background="@color/colorPrimaryDark"
    android:layout_alignParentBottom="true"/>

但wrap_内容参数不起作用,视图占用了最大选项卡宽度

以下是我的TabLayout xml:

        <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@color/background_primary"
        app:tabIndicatorHeight="0dp"
        app:tabMode="scrollable"
        app:tabGravity="fill"
        app:tabPaddingBottom="0dp"
        app:tabPaddingTop="0dp"
        app:tabPaddingStart="0dp"
        app:tabPaddingEnd="0dp"
        android:layout_gravity="bottom"/>

如何为TabLayout选项卡创建适合内部文本的自定义视图(特别是当内部字符串较短时)

也许是一个更好的方法来实现这个结果

给标签充气:

LayoutInflater inflater = getActivity().getLayoutInflater();
RelativeLayout tab1 = (RelativeLayout) inflater.inflate(R.layout.component_custom_tab,null);
RelativeLayout tab2 ...
将id添加到分隔符:

<View
    android:id="@+id/divider"
    android:layout_width="wrap_content"
    android:layout_height="7dp"
    android:background="@color/colorPrimaryDark"
    android:layout_alignParentBottom="true"/>
在表格布局中添加选项卡:

mTabLayout.addTab(mTabLayout.newTab().setCustomView(tab1), index1);
mTabLayout.addTab(mTabLayout.newTab().setCustomView(tab2), index2);
...
将标签充气:

LayoutInflater inflater = getActivity().getLayoutInflater();
RelativeLayout tab1 = (RelativeLayout) inflater.inflate(R.layout.component_custom_tab,null);
RelativeLayout tab2 ...
将id添加到分隔符:

<View
    android:id="@+id/divider"
    android:layout_width="wrap_content"
    android:layout_height="7dp"
    android:background="@color/colorPrimaryDark"
    android:layout_alignParentBottom="true"/>
在表格布局中添加选项卡:

mTabLayout.addTab(mTabLayout.newTab().setCustomView(tab1), index1);
mTabLayout.addTab(mTabLayout.newTab().setCustomView(tab2), index2);
...