如何使用具有不同权重(如百分比宽度)的内容填充布局



>我有水平布局,总重量为 10。我想在上面放一些不同宽度的内容。我希望我的内容看起来像它的宽度的下一个比例:3:5:1:1即像一个列为 30%、50%、10%、10% 的表格如何组织?现在我有:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    android:baselineAligned="true"
    android:clipChildren="false"
    android:orientation="horizontal"
    android:useLargestChild="true"
    android:weightSum="10" >

以及具有不同权重的文本视图,如下所示:

<TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top"
        android:layout_weight="3"
        android:padding="2dp"
        android:text="TextView 1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

但它没有用。视图位于屏幕外。如果我将match_parent更改为wrap_content它看起来不错,而我不在其上放置大文本,那么 TextView 就会长大。我不想要它,我只想显示文本的那部分,它将换行在当前的 30、50 或等 %。

android:layout_width="match_parent"设置为android:layout_width="0dp"

我在容器中的限制文本中找到了解决方案

android:maxEms="<Wieght of this TextView>"

似乎工作正常。但我不确定它是否正确。

最新更新