【问题标题】:Align two textviews so that the right one resizes until max and the left one fits in对齐两个文本视图,使右边的调整大小直到最大,左边的适合
【发布时间】:2016-06-27 21:22:17
【问题描述】:

我在 Android 中需要一个类似表格的视图,我可以用自定义的名称-值行来填充它。一行由两个水平放置的文本视图组成,其中包含名称和值。每行应具有以下属性:

  • 左侧文本视图与父左对齐,右侧文本视图与父右对齐。
  • 正确的文本永远不会超过 7 em,但可以更小。
  • 左边的文本应该填满行中的剩余空间。
  • 显然,两个文本视图不应重叠。

目前我正在尝试使用 LinearLayout 作为容器和两个 TextView 包装在 RelativeLayout 中来解决这个问题,但是这里有些地方明显不合适(下划线的位明显超过 7 ems):


我的 XML:

行布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="4dp"
    android:paddingTop="4dp">

    <TextView
        android:id="@+id/tb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:ellipsize="end"
        android:maxEms="7"
        android:maxLines="2" />

    <TextView
        android:id="@+id/tb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:ellipsize="end"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/tb2"
        android:layout_toStartOf="@id/tb2" />
</RelativeLayout>

以编程方式添加行的容器布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:padding="16dp"/>

我将此布局用作对话框的一部分。不确定它是否会增加问题。

有什么想法/解决方案吗?

【问题讨论】:

    标签: android android-layout textview


    【解决方案1】:

    您不需要做任何特别的事情,只需使用LinearLayout 而不是RelativeLayoutlayout_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="wrap_content"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingBottom="4dp"
        android:paddingTop="4dp">
    
        <TextView
            android:id="@+id/tb2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxEms="7"
            android:maxLines="2" />
    
        <TextView
            android:id="@+id/tb1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:ellipsize="end" />
    </LinearLayout>
    

    也许在右边文本的左边留一些边距也不错。

    【讨论】:

    • 感谢您的回答。我刚刚检查过它,它似乎将左侧文本视图保持在最大 7 ems 的长度,而不是右侧。我试图反转它,但它不起作用 - 小部件仍然太宽,看起来像在屏幕截图上。
    • 我刚刚对其进行了测试,它对这两个领域都非常有效。确保不要将 maxEms 与 layout_weight 一起使用。这种组合没有意义,重量设置会覆盖所有关于假定宽度的限制。
    【解决方案2】:

    它将根据您的要求工作,您可以调整权重

    <?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="wrap_content"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:orientation="horizontal">
    
    
    
        <TextView
            android:id="@+id/tb1"
            android:layout_width="0dip"
            android:layout_weight="2.5"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:ellipsize="end"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/tb2"
            android:text="nameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
            android:layout_toStartOf="@id/tb2" />
        <TextView
            android:id="@+id/tb2"
            android:layout_width="0dip"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:ellipsize="end"
            android:maxEms="7"
            android:maxLines="2"
            android:text="valuessss"/>
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      以上所有答案都是正确的,但您必须了解 RelativeLayout 将组件相对于彼此对齐,例如(左、右、上、下)以达到要求的结果,您必须限制第二个组件的宽度,您的第二个组件存在一个问题即使第一个组件为空,也将始终具有固定宽度。

      LinearLayout 对齐组件,它们在 xml 中呈现的方式意味着线性地一个接一个。 weight 属性告诉布局组件应该在屏幕上占据多少空间。较大的权重值允许它扩展以填充父视图中的任何剩余空间。

      【讨论】:

      • 让我确认一下:您的意思是重量较大的文本视图会自动调整大小以占用权重较小的文本视图留下的所有空间吗?例如如果较小的有文字“a”,那么较大的可以超出仅由重量决定的尺寸吗?
      • 是的,查看文档以获得更好的理解。
      • 在这种特殊情况下使用 LinearLayout 非常有意义。如果可能,尽量避免使用 RelativeLayout:不仅因为 ID 引用更难维护,而且在实际布局完成时,操作系统也会花费更多精力来计算坐标。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      相关资源
      最近更新 更多