Android ListView项目未正确扩展其背景

时间:2014-05-04 23:21:22

标签: android xml listview layout android-listview

我有一个带有9补丁图像的ListView作为背景。列表中的项目看起来像我想要的那样,但是一旦文本达到一定数量的行,它就会将文本下方的文本推出并被切断。我希望文本能够使列表项的9补丁背景延伸到这一点,但它看起来像这样:

enter image description here

这是列表项的XML:

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

    <LinearLayout 
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/arrowUp"
            android:src="@drawable/arrowup"
            android:layout_width="25dp"
            android:layout_height="35dp"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="4dp"
            android:layout_marginRight="4dp"
            android:layout_gravity="center_horizontal"
            android:clickable="true"
            />

        <TextView
            android:id="@+id/scoreText"
            android:layout_width = "wrap_content"
            android:layout_height="wrap_content"
            android:text="0"
            android:textColor="@color/darkgray"
            android:layout_gravity="center_horizontal"
            />

        <ImageView
            android:id="@+id/arrowDown"
            android:src="@drawable/arrowdown"
            android:layout_width="25dp"
            android:layout_height="35dp"
            android:layout_marginLeft="4dp"
            android:layout_marginTop="0dp"
            android:layout_marginRight="4dp"
            android:layout_marginBottom="8dp"
            android:layout_gravity="center_horizontal"
            android:clickable="true"
            />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >

        <TextView 
            android:id="@+id/messageText"
            android:text="This is me testing a large message This is me testing a large message This is me testing a large message This is me testing a large message "
            android:textSize="18sp"
            android:textColor="@color/darkgray"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:layout_marginRight="8dp"/>

        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView 
                android:id="@+id/commentText"
                android:text="0 comments"
                android:textSize="15sp"
                android:textColor="@color/darkgray"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginTop="2dp"
                android:layout_marginBottom="8dp"
                android:layout_gravity="right|bottom"
                android:gravity="left|bottom"
                android:layout_marginRight="8dp"/>

            <TextView 
                android:id="@+id/timeText"
                android:text="14 hours ago"
                android:textSize="15sp"
                android:textColor="@color/darkgray"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="2dp"
                android:layout_marginBottom="8dp"
                android:layout_gravity="right|bottom"
                android:gravity="right|bottom"
                android:layout_marginRight="8dp"/>

        </LinearLayout>


    </LinearLayout>



</LinearLayout>

我意识到要修复此问题我需要将第三个LinearLayout的高度更改为“wrap_content”,但唯一的问题是我希望TextViews timeText和commentText始终位于卡片的底部,这就是为什么它目前是match_parent。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

Hey man check this out if it helps:

change drawables and colors according to your needs.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/appwidget_bg"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/arrowUp"
                android:layout_width="25dp"
                android:layout_height="35dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_marginTop="4dp"
                android:clickable="true"
                android:src="@drawable/icon" />

            <TextView
                android:id="@+id/scoreText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="0"
                android:textColor="@android:color/darker_gray" />

            <ImageView
                android:id="@+id/arrowDown"
                android:layout_width="25dp"
                android:layout_height="35dp"
                android:layout_gravity="center_horizontal"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="4dp"
                android:layout_marginRight="4dp"
                android:layout_marginTop="0dp"
                android:clickable="true"
                android:src="@drawable/icon" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/messageText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="8dp"
                android:layout_marginTop="4dp"
                android:text="This is me testing a large message This is me testing a large message This is me testing a large message This is me testing a large message "
                android:textColor="@android:color/darker_gray"
                android:textSize="18sp" />
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/commentText"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="right|bottom"
            android:layout_marginBottom="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="2dp"
            android:gravity="left|bottom"
            android:text="0 comments"
            android:textColor="@android:color/darker_gray"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/timeText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="right|bottom"
            android:layout_marginBottom="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="2dp"
            android:gravity="right|bottom"
            android:text="14 hours ago"
            android:textColor="@android:color/darker_gray"
            android:textSize="15sp" />
    </LinearLayout>

</LinearLayout>