Android trouble with listview row XML layout
I've got a ListView with a custom adapter, and I'm trying to modify the layout XML for the list item row so that it looks correct. I'm trying to achieve the following layout (imagine that the below is centered vertically):
Icon Title >
Description
The problem that I'm running into however is that the Title and Description's are varying lenghts, therefore the ">" icon is pushed more to the right with longer text (and sometimes goes beyond the screen).
I'm trying to get the ">" icon to always be on the far right edge of the screen, and to have any longer text be "capped" with an ellipsis. I'd prefer to not use a RelativeLayout. Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" >
<ImageView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:src="@drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/serverName"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:textSize="16sp" />
<TextView
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingRight="10dp"
android:paddingLeft="15dp"
android:textSize="10sp"
android:singleLine="true"
android:ellipsize="end" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:gravity="right"
android:src="@dra开发者_C百科wable/chevron" />
</LinearLayout>
I'd appreciate any help.
Use RelativeLayout to achieve this. Here is an example from android developer blog LINK
Put your icon in another LinearLayout
Just ignore and replace your icon and text colors because it was my formation.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal" android:layout_height="wrap_content">
<LinearLayout android:layout_width="fill_parent" android:padding="5sp" android:layout_height="fill_parent" android:layout_weight="5">
<ImageView android:id="@+id/news_image" android:layout_height="50sp" android:layout_width="50sp" android:scaleType="center" android:src="@drawable/my_icon" />
</LinearLayout>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="8sp" android:layout_weight="2">
<TextView android:text="News Heading" android:id="@+id/news_heading" android:layout_height="wrap_content" android:textSize="20sp" android:ellipsize="end" android:maxLines="1" android:layout_width="fill_parent" android:textColor="@color/themePlainBlue"></TextView>
<TextView android:text="News Details" android:id="@+id/news_details" android:layout_height="wrap_content" android:ellipsize="end" android:maxLines="1" android:layout_width="fill_parent"></TextView>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="5.6">
<ImageView android:id="@+id/news_image" android:layout_height="50sp" android:scaleType="center" android:src="@drawable/list_arrow" android:layout_width="32sp" android:layout_gravity="center_vertical|center_horizontal"></ImageView>
</LinearLayout>
</LinearLayout>
精彩评论