【问题标题】:Aligning two textviews in a listview在列表视图中对齐两个文本视图
【发布时间】:2016-07-05 02:52:42
【问题描述】:

这是我所拥有的:

有选项,每个选项都有内容。选项编号(A、B、C、D)在单独的文本视图中,它们的内容在单独的文本视图中。

我正在尝试将选项编号与选项内容的第一行对齐。我尝试设置常量paddingTop,但随着选项内容中的行数发生变化,它会失去对齐。有什么建议吗?

这是 xml 的一部分:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/options"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">

    <TextView
        android:id="@+id/option_number"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="10"
        android:text="A"
        android:paddingLeft="5dp"
        android:textSize="17sp"
        android:textStyle="bold"
        android:textColor="@color/charcoal"/>



    <TextView
        android:id="@+id/option_content"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="100"
        android:textSize="17sp"
        android:gravity="center|left"
        android:paddingRight="20dp"
        android:text="option content...."
        android:textColor="@color/charcoal"/>

我可以通过动态获取选项内容的paddingTop 并为选项编号设置相同的填充来做到这一点吗?

【问题讨论】:

    标签: android listview padding


    【解决方案1】:

    尝试在option_content上设置android:layout_alignBaseline="@id/option_number"

    【讨论】:

    • 感谢您的回复,但我们可以将它与LinearLayout 一起使用吗?
    • 我的错。从我看到的代码中,您应该可以更改为 relativelayout :)
    • 好吧,我也有一个ImageView。所有这些都使用layout_weight 按比例分布。如果我使用相对布局,我将无法使用它。
    【解决方案2】:

    您可以使用相对布局而不是线性布局,但您必须稍微更改您的 xml。因此,如果您决定使用它,您只需将下一个属性设置为第一个 TextView

    android:layout_alignTop="@+id/option_content"
    

    它应该对你有帮助。

    【讨论】:

    • 使用RelativeLayout 不允许我使用layout_weight
    • @user5038993 如果您确实需要仅使用 LinearLayout,请在您的问题中说明这一点,以避免人们提交的答案确实解决了您提出的问题,但不是您真正想要的。
    【解决方案3】:

    为 option_number 设置 android:gravity="top" TextView 为 option_content 设置 android:gravity="top|left"。或者,使用相对布局并使用 layout_aligntop

    【讨论】:

    • 无法使用选项内容的顶部重力。它必须漂浮在中间。
    • 在这种情况下使用相对布局。使用 layout_gravity 作为 "center|left" 作为选项内容,使用 layout_alignTop="@id/option_content" 作为选项编号
    • 好的,但话又说回来,我将无法使用 layout_weight 属性。
    【解决方案4】:

    使用:android:layout_centerVertical="true"

    建议,可以结合使用以下三个来轻松实现你想要的:
    1) 垂直居中
    2) ToRightOf
    3) 相对布局。

    例如:

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/options"
        android:layout_width="match_parent"
        android:layout_height="80dp">
    
        <TextView
            android:layout_centerVertical="true"
    
            android:id="@+id/option_number"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:text="A"
            android:paddingLeft="5dp"
            android:textSize="17sp"
            android:textStyle="bold"
            android:textColor="@color/charcoal"/>
    
        <TextView
            android:layout_toRightOf="@id/option_number"
            android:layout_centerVertical="true"
    
            android:id="@+id/option_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="17sp"
            android:text="option content...."
            android:textColor="@color/charcoal"/>
    

    【讨论】:

    • 那么layout_weight 呢?
    • 如果您使用的是RelativeLayout,我不明白为什么您需要在此处使用“layout_weight”。您可以将@id/option_content 设置为toRightOf @id/option_number。 (并为@id/option_number 使用固定宽度)
    • 哦,对不起,这将使两个文本视图在列表视图中垂直居中,看起来不错。如果您确实希望第一个文本框与第二个列表框的 第一行文本 对齐,则在您的第一个文本视图中添加 "android:alignTop="@id/option_content"。并在您的第二个文本视图中,删除“toRightOf”,而只需添加 layout_marginLeft
    【解决方案5】:

    试试下面的。

    在您的 xml 中替换它。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/options"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:weightSum="5">
    
            <TextView
                android:id="@+id/option_number"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:paddingLeft="5dp"
                android:text="A"
                android:maxLines="4"
                android:textColor="@color/charcoal"
                android:textSize="17sp"
                android:textStyle="bold" />
    
    
            <TextView
                android:id="@+id/option_content"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:maxLines="4"
                android:text="option content...."
                android:textColor="@color/charcoal"
                android:textSize="17sp" />
    
        </LinearLayout>
    

    完成了。

    肯定会对你有所帮助。

    【讨论】:

    • 谢谢,答案很接近。但是每当有多行时,选项编号就会比选项内容低一点。有什么建议吗?
    • @user5038993 你能告诉我在多行时它显示得低多少吗? Bcz 在我的布局中,我尝试过即使有多行,它也不会显示得更低。
    • @user5038993 如果对您有帮助,请采纳答案谢谢:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 2019-10-28
    • 2011-06-26
    • 1970-01-01
    相关资源
    最近更新 更多