AbsListView LayoutParams设置布局重力

nkn*_*knj 2 layout android gravity layoutparams

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

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

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

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

编辑:(添加源代码)

Listview Parent

<?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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我应该在我的适配器的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" >
Run Code Online (Sandbox Code Playgroud)

第二个线性布局没用,但让它完成工作!

Luk*_*rog 5

要更改重力,您可以将现有的行布局包装在另一个布局中(FrameLayout例如),然后只需LinearLayout从该额外布局(现在可以在父级内部移动)中设置重力.

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