带有两个选项卡的 TabLayout 一个 75% 其他 25%

TabLayout with two tabs one 75% other 25%

我在有两个选项卡的屏幕上工作:

示例:

这是我定义 TabLayout 的方式:

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMaxWidth="0dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/orange"
app:tabTextAppearance="@style/TabTextAppearance"
app:tabTextColor="@color/darkGrey" >

如何自定义选项卡布局以固定所需的宽度?

您可以通过编程方式完成。为不同的标签分别添加它们。

LinearLayout layout = ((LinearLayout) ((LinearLayout) tabLayout.getChildAt(0)).getChildAt(YOUR_TAB_NUMBER));
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams();
layoutParams.weight = YOUR_WEIGHT; // e.g. 0.5f
layout.setLayoutParams(layoutParams);

对于LinearLayout你可以使用weight机制:

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

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:background="#0f0" />

    <View
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#00F" />

</LinearLayout>

第一个 View 的权重 3,第二个 1。所以加起来有4(前75%,后25%)