2

如何为 AbsListView.LayoutParams 设置布局重力?

我在自定义数组适配器中使用它,我需要根据变量设置布局重力。我的列表子项包含一个线性布局根,但在适配器中,我无法设置 LinearLayout 参数,因为我收到无法将 LinearLayout.LayoutParams 强制转换为 AbsListView.Layout 参数的错误。

因此,我尝试为 AbsListView 设置 layout_gravity,但它没有重力选项。

在这种情况下,如何以编程方式设置 layout_gravity?

编辑:(添加源代码)

Listview 父级

<?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="match_parent"
    android:background="@drawable/messaging_background"
    android:orientation="vertical"
    android:padding="2dp" >

    <ListView
        android:id="@+id/messageThread"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/list_separator"
        android:layout_alignParentTop="true"
        android:layout_marginTop="5dp"
        android:cacheColorHint="#00000000"
        android:divider="@null"
        android:dividerHeight="2dp"
        android:fastScrollEnabled="false"
        android:listSelector="#00000000"
        android:transcriptMode="disabled" />

    <View
        android:id="@+id/list_separator"
        android:layout_width="match_parent"
        android:layout_height="1.0dip"
        android:layout_above="@+id/footer"
        android:background="@android:color/white" />

    <LinearLayout
        android:id="@+id/footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/filter"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.1"
            android:background="@null"
            android:contentDescription="@string/message_filter_button"
            android:scaleType="fitCenter"
            android:src="@drawable/filter" />

        <EditText
            android:id="@+id/send_message_text"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:imeOptions="actionSend"
            android:inputType="textMultiLine"
            android:maxLines="6"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:scrollbarDefaultDelayBeforeFade="200"
            android:scrollbarFadeDuration="300"
            android:scrollbarStyle="outsideOverlay"
            android:scrollbars="vertical" />

        <ImageButton
            android:id="@+id/send"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="0.1"
            android:background="@null"
            android:contentDescription="@string/send_message_button"
            android:scaleType="fitCenter"
            android:src="@drawable/send_btn" />
    </LinearLayout>

</RelativeLayout>

Listview 孩子

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@drawable/incoming"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/messaging_bubble_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:fontFamily="sans-serif-light"
        android:maxWidth="200dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textIsSelectable="true" />

    <TextView
        android:id="@+id/messaging_bubble_time"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:fontFamily="sans-serif-light"
        android:gravity="center"
        android:paddingRight="5dp"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textIsSelectable="false" />

    <ImageView
        android:id="@+id/messaging_bubble_megaphone"
        android:layout_width="20dp"
        android:layout_height="match_parent"
        android:contentDescription="@string/message_child_megaphone"
        android:src="@drawable/megaphone"
        android:visibility="gone" />

</LinearLayout>

我应该在我的适配器的 getView 中调用什么来根据适配器中的变量将孩子向右推?

编辑2:

一个可能的解决方案:将整个子元素包裹在根下的另一个元素下,然后您可以设置重力而不是布局重力,这可以使用该setGravity()方法轻松完成。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/messaging_bubble_wrapper"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/incoming"
        android:orientation="vertical"
        tools:ignore="UselessParent" >

第二个线性布局是无用的,但它可以完成工作!

4

4 回答 4

5

要更改重力,您可以将现有的行布局包装在另一个布局中(FrameLayout例如),然后简单地设置LinearLayout来自该额外布局的重力(现在可以在父布局内​​移动)。

为了避免在布局中添加额外的级别,您可以保留当前行布局并LinearLayout用 a替换根,RelativeLayout并根据您所需的重力更改其中视图的位置。

于 2013-07-04T04:35:56.837 回答
2

我通过在我的根元素下创建另一个“无用视图(用 Eclipse 的话)”解决了这个问题。

我将父级的宽度设置为 match_parent,然后更改重力以向左或向右移动“无用视图”。该setGravity(Gravity.LEFT)方法在AbsListView.

于 2013-07-04T03:12:41.583 回答
1

我认为你需要摆脱 ListView。将 ListView 替换为 FrameLayout 并使用 layoutinflater 将“listview”子项添加到 FrameLayout 中。使用 layoutinflater 时,将 FrameLayout.LayoutParams 的重力属性分配给孩子的根 (LinearLayout)。祝你好运。

于 2013-06-27T11:06:22.420 回答
1

您可以设置孩子的内容的重力setGravity()

View child;
boolean left;

child.setGravity(left ? Gravity.LEFT : Gravity.RIGHT);

而且孩子应该改成android:layout_width="match_parent"

于 2013-06-28T18:15:59.557 回答