android-TabLayout动态调整大小

Den*_*Den 3 android android-tablayout

我的应用程序中有一个TabLayout,它包含自定义视图:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="horizontal">

        <com.myproject.app.utils.commons.IconImageView
            android:id="@android:id/icon"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_marginRight="14dp"
            android:layout_marginEnd="14dp"
            app:iconStateListColor="@color/color_order_tab"/>

        <TextView
            android:id="@android:id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/color_order_tab"
            android:textSize="14sp"
            android:fontFamily="sans-serif-medium"/>

    </LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我只想在选项卡上显示文本(已完成)并动态调整这些选项卡的大小,是否有任何方法可以这样做?如果使用app:tabMode="fixed"选项卡,请不要调整大小,如果使用选项卡,则app:tabMode="scrollable"选项卡始终位于屏幕左侧。如果我 android:layout_width="wrap_content"android:layout_gravity="center_horizontal"- TabLayout没有填满屏幕。所以我需要:

  • 出现或消失文本时调整标签的大小;
  • 在任何情况下,选项卡都应填满屏幕。

Kil*_*ler 6

首先,您无法在使用fixed模式下获得所有所需的功能。与在fixed模式下一样,您无法调整标签的大小。您需要使用scrollable模式才能调整选项卡的大小。

如果我执行android:layout_width =“ wrap_content”和android:layout_gravity =“ center_horizo​​ntal”-TabLayout不会填满屏幕

您可以设置一个父容器并设置一个背景,以使您感觉具有完整设备宽度的选项卡布局。

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/holo_orange_light">

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        app:tabGravity="center"
        app:tabMode="scrollable" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

然后,您可以在选项卡布局中添加选项卡,即自定义选项卡,然后,可以添加addOnTabSelectedListener以获取所需的结果:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        View view = getLayoutInflater().inflate(R.layout.custom_tab, null);
        ((TextView) view.findViewById(R.id.text)).setText("large text one");

        View view1 = getLayoutInflater().inflate(R.layout.custom_tab, null);
        ((TextView) view1.findViewById(R.id.text)).setText("1");

        View view2 = getLayoutInflater().inflate(R.layout.custom_tab, null);
        ((TextView) view2.findViewById(R.id.text)).setText("Large Text Text Text");

        View view3 = getLayoutInflater().inflate(R.layout.custom_tab, null);
        ((TextView) view3.findViewById(R.id.text)).setText("One");


        View view4 = getLayoutInflater().inflate(R.layout.custom_tab, null);
        ((TextView) view4.findViewById(R.id.text)).setText("Another large text");

        mBinding.tabs.addTab(mBinding.tabs.newTab().setCustomView(view));
        mBinding.tabs.addTab(mBinding.tabs.newTab().setCustomView(view1));
        mBinding.tabs.addTab(mBinding.tabs.newTab().setCustomView(view2));
        mBinding.tabs.addTab(mBinding.tabs.newTab().setCustomView(view3));
        mBinding.tabs.addTab(mBinding.tabs.newTab().setCustomView(view4));

        mBinding.tabs.getTabAt(0).getCustomView().findViewById(R.id.text).setVisibility(View.VISIBLE);

        mBinding.tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.text).setVisibility(View.VISIBLE);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.text).setVisibility(View.GONE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }
Run Code Online (Sandbox Code Playgroud)

如您所见,这些选项卡现在可以调整大小,并且只显示所选选项卡上的文本。希望对您有所帮助