【问题标题】:Listview items spreading till full width of screenListview 项目扩展到整个屏幕
【发布时间】:2016-05-24 11:25:02
【问题描述】:

我使用列表视图在两个用户之间聊天。我想将接收者发送的聊天消息的背景颜色设置为蓝色,为发送者设置其他颜色。我还想将列表视图项目的大小限制为wrap_content,但它似乎不起作用,因此背景颜色遍布整个项目的宽度。

以下是包含列表视图的活动代码:

<FrameLayout 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"
android:background="@drawable/bg"
tools:context=".ChatScreen">

<include layout="@layout/toolbar" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize">

    <io.codetail.widget.RevealFrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ListView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/lMsgs"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_above="@+id/editText"
                android:divider="@null"/>

            <EditText
                android:text=""
                android:layout_width="wrap_content"
                android:id="@+id/editText"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_toLeftOf="@+id/bSendMsg" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/sendd"
                android:id="@+id/bSendMsg"
                android:layout_below="@+id/lMsgs"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="false"
                android:layout_alignParentBottom="true"/>

        </RelativeLayout>

        <include layout="@layout/media_attach_menu" />

    </io.codetail.widget.RevealFrameLayout>

</FrameLayout>

列表视图项的代码:

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<RelativeLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_gravity="left"
android:id="@+id/RelativeLayout">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:textSize="16sp"
    android:text="Username"
    android:id="@+id/tvUsername"
    android:layout_alignParentTop="true"
    />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/tvUsername"
    android:id="@+id/innerRL">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tvMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_alignParentTop="false"
        android:text="Message"
        />

    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16sp"
        android:textStyle="bold"
        android:visibility="gone"
        android:contentDescription="@string/image"
        android:scaleType="fitCenter"
        android:maxHeight="50dp"
        android:maxWidth="50dp"
        android:minHeight="50dp"
        android:minWidth="50dp"
        />
</RelativeLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:textSize="14sp"
    android:text="Timestamp"
    android:id="@+id/tvTime"
    android:layout_below="@id/innerRL"
    />

</RelativeLayout>

【问题讨论】:

    标签: android listview


    【解决方案1】:

    您将android:background="@drawable/bg" 设置为您的父布局意味着FrameLayout 因此它适用于您的所有视图而不是从那里删除单行,您可以将此背景应用于您的列表视图项目布局

    <?xml version="1.0" encoding="utf-8"?>
    <!--  Single List Item Design -->
    <RelativeLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content"
       android:layout_gravity="left"
       android:background="@drawable/yourbackground"
       android:id="@+id/RelativeLayout">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textSize="16sp"
        android:text="Username"
        android:id="@+id/tvUsername"
        android:layout_alignParentTop="true"
        />
      ----
    

    【讨论】:

    • 所以由于背景的原因,我的列表视图项目会散布在整个宽度上?
    • 不,如果你设置wrap_content,它取决于你的布局宽度,而不是全宽,否则它包含全宽
    • 你能再检查一下我的代码吗?列表视图及其项目的宽度为wrap_content
    • 我按照你说的改变了listview的高度和宽度,但没有帮助
    • 您必须在列表项布局中添加背景,而不是在列表视图布局中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    相关资源
    最近更新 更多