Tablayout setCustomView()不会更改标签的高度

时间:2018-08-31 11:42:38

标签: android android-tablayout

enter image description here

我正在尝试将自定义视图设置为选项卡布局,但选项卡没有达到自定义视图的高度,因此它被截断,有人可以告诉我我在做什么错吗?我试图调用invalidate( )和requestlayput()用于tabLayout,但这没有帮助。

ContactPagerAdapter contactPagerAdapter = new ContactPagerAdapter(getFragmentManager(), new String[]{getString(R.string.received), getString(R.string.given)});
        viewPager.setAdapter(contactPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        TabLayout.Tab tab1 = tabLayout.getTabAt(0);
        tab1.setCustomView(getTabView(0, 0));
        TabLayout.Tab tab2 = tabLayout.getTabAt(1);
        tab2.setCustomView(getTabView(1, 2));


public View getTabView(int position, int count) {
        String[] tabTitles = new String[]{getString(R.string.received), getString(R.string.given)};
        View tabView = LayoutInflater.from(getActivity()).inflate(R.layout.tab_layout, null);
        if (position == 0) {
            receivedCount = tabView.findViewById(R.id.tv_vouch_count);
            receivedCount.setText(String.format("%s", String.valueOf(count)));
        } else {
            givenCount = tabView.findViewById(R.id.tv_vouch_count);
            givenCount.setText(String.format("%s", String.valueOf(count)));
        }
        TextView tabTitle = tabView.findViewById(R.id.tv_tab_title);
        tabTitle.setText(tabTitles[position]);
        return tabView;
    }

tab_layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="wrap_content">

    <TextView
        android:id="@+id/tv_vouch_count"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textColor="@color/action_bar_color"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_tab_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textColor="@color/tv_color"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_vouch_count" />

</android.support.constraint.ConstraintLayout>

tab.xml

 <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="@android:color/transparent"
                app:tabBackground="@drawable/bg_tab"
                app:tabGravity="fill"
                app:tabIndicatorColor="@android:color/holo_orange_dark"
                app:tabMaxWidth="0dp"
                app:tabMode="fixed"
                app:tabSelectedTextColor="@color/tv_color"
                app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
                app:tabTextColor="@color/tv_color" />

5 个答案:

答案 0 :(得分:0)

解决方案:

尝试

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

将TabLayout包裹在FramLayout中,然后尝试。

希望有帮助。

答案 1 :(得分:0)

在dps中设置layout_height而不是wrap_content,这可能在不同的显示尺寸上有所不同,但是如果您想动态设置高度,请选中this

答案 2 :(得分:0)

使用默认android hight的选项卡布局尝试像这样放置真实值

<android.support.design.widget.TabLayout
..
                android:layout_height="50dp"
.. />

答案 3 :(得分:0)

您是否尝试过以这种方式使用它?如果要根据设备的屏幕尺寸使用动态高度,请执行以下操作:

private void initTabLayout() {

    tabsMultipleList.addTab(tabsMultipleList.newTab().setText("" + getString(R.string.tab_1)));
    tabsMultipleList.addTab(tabsMultipleList.newTab().setText("" + getString(R.string.tab_2)));

    tabsMultipleList.setTabGravity(TabLayout.GRAVITY_FILL);
    tabsMultipleList.setPadding(0, 0, 0, 0);
  

//下面的行是真正的解决方法,它将根据您的屏幕大小调整标签的大小。

    tabsMultipleList.setSelectedTabIndicatorHeight((int) (2 * getResources().getDisplayMetrics().density));
    //tabsMultipleList.setSelectedTabIndicatorColor(Color.parseColor("#0070C0"));
    int accentColor = ResourcesCompat.getColor(getResources(), R.color.colorAccent, null);

    tabsMultipleList.setSelectedTabIndicatorColor(accentColor);


    TextView tabOne = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null);
    tabOne.setText("" + getString(R.string.tab_1));
    tabOne.setTextColor(accentColor);
    tabOne.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL);
    tabsMultipleList.getTabAt(0).setCustomView(tabOne);

    TextView tabTwo = (TextView) LayoutInflater.from(getActivity()).inflate(R.layout.custom_tab, null);
    tabTwo.setText("" + getString(R.string.2));
    tabTwo.setTextColor(accentColor);
    tabTwo.setGravity(Gravity.CENTER | Gravity.CENTER_HORIZONTAL);
    tabsMultipleList.getTabAt(1).setCustomView(tabTwo);
}

答案 4 :(得分:0)

添加customView后,应该刷新TabLayout的layoutParams,但是我无法计算出新的大小,并且我使用固定大小对其进行了测试:

tabLayout.setLayoutParams(new ConstraintLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,190));

ContactPagerAdapter contactPagerAdapter = new ContactPagerAdapter(getFragmentManager(), new String[]{getString(R.string.received), getString(R.string.given)});
        viewPager.setAdapter(contactPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        TabLayout.Tab tab1 = tabLayout.getTabAt(0);
        tab1.setCustomView(getTabView(0, 0));
        TabLayout.Tab tab2 = tabLayout.getTabAt(1);
        tab2.setCustomView(getTabView(1, 2));

        tabLayout.setLayoutParams(new ConstraintLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 190 ));

我希望它可以帮助您更好地理解它。