TabLayout有两个标签,一个75%,另外25%

问题描述 投票:-1回答:2

我正在一个有两个标签的屏幕上工作:

  • 第一个必须具有屏幕宽度的75%。
  • 第二个必须具有屏幕宽度的25%。

例:

enter image description here

这就是我定义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" >

如何自定义标签布局以固定所需的宽度?

android tabs android-tablayout
2个回答
0
投票

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

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

-1
投票

对于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%)

© www.soinside.com 2019 - 2024. All rights reserved.