【问题标题】:LinearLayout used as item layout in ListView won't go to the right在 ListView 中用作项目布局的 LinearLayout 不会向右
【发布时间】:2015-12-20 03:59:30
【问题描述】:

我正在尝试将一些 ListView 项目向右对齐,但它们不会从左侧移动。重力和 layout_gravity 属性似乎不起作用。

此代码还将布局与 XML 代码中的右侧对齐,但不会在运行时填充 ListView。

这是我正在使用的 XML,它也使用了 9 个补丁图像作为 LinearLayout 的背景:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="0dp"
    android:background="@drawable/my_9Patch_image"
    android:id="@+id/contentLayout"
    android:orientation="horizontal"
    android:layout_gravity="right"
    android:gravity="right"

    >

    <TextView
        android:layout_width="wrap_content"
        android:maxWidth="330dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right"
        android:textColor="#e5e5e5"
        android:minHeight="0dp"
        android:singleLine="false"
        android:textSize="16dp">


    </TextView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:textColor="#e5e5e5"
        android:minHeight="0dp"
        android:textSize="10dp"
        android:layout_gravity="left|bottom"
        android:gravity="left|bottom"

        />

</LinearLayout>

我还尝试使用以下方法在适配器的 getView 方法中设置参数:

 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.RIGHT;
            mLinearLayout.setLayoutParams(params);

提前感谢您的帮助。

【问题讨论】:

  • 你能试着描述一下你想要完成的事情吗?
  • 感谢您的回复。我正在构建一个聊天应用程序,对于传入的消息,消息在左侧,发送的消息在右侧。每个消息布局都包含一个消息和时间戳。我使用两种不同的 XML 布局,并在适配器的 getView 方法中扩展适当的布局(传入或传出消息布局)。谢谢
  • 不,我问的是你想看的布局?你想在左边有一个 TextView 和一个向右对齐。这是真的吗?
  • 是的,我想要一个 LinearLayout 作为聊天气泡的形状,然后 TextView 中的消息文本在气泡中从左到右,对于传出消息,要有包含 TextView时间戳位于左下角。
  • 你知道相对布局吗?

标签: android listview alignment android-linearlayout nine-patch


【解决方案1】:

初步解决方案(我会回来的)。这似乎效率低下,但您可以添加另一个 LinearLayout 来包装另一个 LinearLayout。取容器 LinearLayout 并将高度和宽度都设置为 match_parent,并将重力属性放在右边。然后将 9 补丁图像添加到嵌套的 LinearLayout 中,而不是将其放在父容器 LinearLayout 视图级别,并将其高度和宽度更改为 wrap_content。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/contentLayout"
            android:orientation="horizontal"
            android:gravity="right"
               >

            <LinearLayout android:layout_height="wrap_content"
             android:layout_width="wrap_content"
             android:background="@drawable/purple_right_2"
            >


    <TextView
        android:id="@+id/message_outgoing"
        android:layout_width="wrap_content"
        android:maxWidth="330dp"
        android:layout_height="wrap_content"
        android:textColor="#e5e5e5"
        android:minHeight="0dp"
        android:singleLine="false"
        android:textSize="16dp">

    </TextView>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:id="@+id/clockView"
        android:textColor="#e5e5e5"
        android:minHeight="0dp"
        android:textSize="10dp"
        android:layout_gravity="left|bottom"
                    />

    </LinearLayout>
    </LinearLayout>

【讨论】:

    【解决方案2】:

    我会查看http://www.vogella.com/tutorials/AndroidListView/article.html 中的rowbuttonlayout.xml

    基本上,您正在尝试更改项目的重力,但您正在尝试从列表视图的外部。可能它试图通过将整个列表视图向右移动来坚持,但列表视图占用了所有空间,所以你甚至不会注意到它 如果您希望单个元素左对齐,您将需要制作左对齐的自定义单元格,在您想要左对齐时制作,或者制作您自己的单元格,并在必要时左对齐该单元格中的内容

    【讨论】:

    • 谢谢。我会读一读并试一试。但是你能澄清一下你所说的合理吗?
    • 与文本文档的对齐方式相同。它是文本附着在哪一侧或是否居中。
    • 当我使用开发者打开“显示布局边界”时。选择我可以看到 ListView 行只占用了与视图中的包装内容而不是整个父级一样多的空间。当您说自定义单元格时,您的意思是 row 吗?我确实尝试过,使用 LinearLayout 和 RelativeLayout。它仅在我添加另一个父容器时才有效。我也看过你发给我的链接,似乎他们正在将父级中包含的视图对齐到左侧的右侧,这对我有用,但是父级容器不会向右或向左移动,没有问题所在的包装器。谢谢
    • 当我说单元格时,我的意思是行。我糊涂了。我在制作自定义行时根据您对问题的描述发布了此内容,您可能会将它们作为视图,其中包含视图。列表内的视图将以“match_parent”作为宽度,然后设置重力设置,使其粘在各自的一侧。这将允许行项目粘在列表视图的一侧。如果您试图让整个列表视图粘在屏幕的一侧(并在另一侧留出一些空间),您将需要一个相对视图并限制列表视图的宽度
    • 另外更改列表视图的重力将移动列表视图,但不会影响列表视图中各个行项目的重力。要影响行项目的重力,您需要控制行项目的视图。为此,您可以轻松创建自定义行视图,而不是默认行视图。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2020-02-11
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多