Android:TabLayout如何以编程方式设置height = 0?

时间:2017-11-06 10:33:10

标签: java android android-tablayout

此处xml TabLayout

<?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="match_parent">


    <android.support.design.widget.TabLayout
         android:id="@+id/tabLayout"
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_marginTop="20dp"
         android:background="@android:color/white"
         android:minHeight="@dimen/standard_min_height_container"
         app:layout_constraintLeft_toLeftOf="parent"
         app:layout_constraintRight_toRightOf="parent"
         app:tabGravity="fill"
         app:tabIndicatorHeight="0dp"
         app:tabMode="fixed"
         app:tabSelectedTextColor="@color/colorPrimaryDark"/>

<ViewPager
    android:id="@+id/customViewPager"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:layout_constraintLeft_toLeftOf="@+id/tabLayout"
    app:layout_constraintRight_toRightOf="@+id/tabLayout"
    app:layout_constraintTop_toBottomOf="@+id/tabLayout" />

</android.support.constraint.ConstraintLayout>

如何以编程方式更改 TabLayout.android:layout_height = 0

1 个答案:

答案 0 :(得分:1)

试试这个

  • 对于Constraint Layout,您只需更改高度和宽度

    即可
    tabLayout=(TabLayout)findViewById(R.id.tabLayout);
        ConstraintLayout.LayoutParams params = 
      (ConstraintLayout.LayoutParams) tabLayout.getLayoutParams();
      // Changes the height and width to the specified *pixels*
        params.height = 500;
        params.width = 500;
        tabLayout.setLayoutParams(params);
    

输出

enter image description here