设计Android ListView?

时间:2012-07-10 13:39:47

标签: android android-layout android-listview android-styles

我需要为Android应用程序设计ListView样式。我试图查找如何做这种类型的样式,但我想我正在搜索错误的项目。

我的数据由ContentProvider返回的对象组成,CursorLoader用于访问数据。数据将按日期包含节标题。一个部分中的每个项目都有一个类别,我想使用该类别的颜色代码为最左边的边框(边距或填充)着色。请参阅下面的布局xml。细节行由3行数据组成,左下角有一个按钮。

请参阅此Google绘图,了解列表的外观 Drawing of how I want to style the ListView

这种格式是否可行?

感谢您的帮助。

<?xml version="1.0" encoding="utf-8"?>
<!-- 
 *  This file is part of MythTV for Android
 * 
 *  MythTV for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MythTV for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MythTV for Android.  If not, see <http://www.gnu.org/licenses/>.
 *   
 * This software can be found at <https://github.com/MythTV-Android/mythtv-for-android/>
 *
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/upcoming_header_row"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <TextView
        android:id="@+id/upcoming_header_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/body_text_1" />

</LinearLayout>

<LinearLayout
    android:id="@+id/upcoming_detail_row"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <TextView
        android:id="@+id/upcoming_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/body_text_1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/upcoming_sub_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/body_text_1" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="8dp"
            android:paddingRight="8dp" >

            <TextView
                android:id="@+id/upcoming_channel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:textColor="@color/body_text_1" />

            <TextView
                android:id="@+id/upcoming_start_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:textColor="@color/body_text_1" />

            <Button
                android:id="@+id/upcoming_dont_record"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="@string/btn_upcoming_dont_record"
                android:textColor="@color/body_text_1"
                android:textSize="@dimen/text_size_small" />

    </RelativeLayout>

</LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

是..为upcoming_detail_row使用3个不同的背景图片 在您的代码中,根据某些条件动态设置该LinearLayout的背景图像。

答案 1 :(得分:0)

你只需要水平方向的另一个LinearLayout,然后是一个视图来保持颜色。将现有的coming_detail_row作为第二个元素嵌套在新的LinearLayout中,将新的View(作为保持颜色)作为第一个元素嵌套。

然后您可以以编程方式设置该视图的背景颜色。

以下是根据您的布局改编的示例。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:id="@+id/upcoming_header_row"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="8dp"
    android:paddingRight="8dp" >

    <TextView
        android:id="@+id/upcoming_header_label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000" />
</LinearLayout>

<LinearLayout
    android:id="@+id/newLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <View
        android:id="@+id/view1"
        android:layout_width="10dp"
        android:layout_height="fill_parent"
        android:background="#ff0000" />

    <LinearLayout
        android:id="@+id/upcoming_detail_row"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="8dp"
        android:paddingRight="8dp" >

        <TextView
            android:id="@+id/upcoming_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="title"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#000000" />

        <TextView
            android:id="@+id/upcoming_sub_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="subtitle"
            android:textColor="#000000" />

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingLeft="8dp"
            android:paddingRight="8dp" >

            <TextView
                android:id="@+id/upcoming_channel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:text="channel"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/upcoming_start_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="startTime"
                android:textColor="#000000" />

            <Button
                android:id="@+id/upcoming_dont_record"
                android:layout_width="40dp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="button"
                android:textColor="#000000"
                android:textSize="14sp" />
        </RelativeLayout>
    </LinearLayout>
</LinearLayout>