2012-07-10 58 views
0

我需要一些幫助樣式化Android應用程序的ListView。我想查找如何做這種類型的樣式,但我想我正在尋找錯誤的項目。樣式化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> 
+0

的形象就是你的列表項或ListView? – 2012-07-10 13:43:03

+0

@SoumyadipDas該圖片是我想要的樣子。實際的顯示效果很好。列表標題和詳細信息按預期顯示。在不需要時關閉標題,只顯示一個新的部分。我只是不確定如何在左側添加着色。 – dmfrey 2012-07-10 13:47:00

+0

我確實認爲這隻會添加到帶有id @ id/auctions_detail_row – dmfrey 2012-07-10 13:48:58

回答

0

您只需要在水平方向上使用另一個LinearLayout,然後使用View來保持顏色。將新的LinearLayout作爲第二個元素,將新的View(保持顏色)作爲第一個元素嵌套在現有的browsing_detail_row中。

然後,您可以編程方式設置該視圖的背景顏色。

以下是根據您的佈局調整的示例。

<?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> 

+0

謝謝。這看起來完全像我在找的東西。會給它一個鏡頭。 – dmfrey 2012-07-10 15:57:20

+0

好吧,如果它有效,請務必將其標記爲您的答案。 – Fraggle 2012-07-10 19:19:36

+0

工作很好。謝謝。 – dmfrey 2012-07-10 23:34:32

0

是..使用3張不同的背景圖片爲upcoming_detail_row 在你的代碼爲動態基於某些條件的LinearLayout的背景圖像。

+0

的LinearLayout,因此必須使用圖像來完成此操作?它不能使用填充顏色?我只關心那麼可能沒有填補不同設備上的排的高度。 – dmfrey 2012-07-10 14:12:34

+0

我不確定,因爲我也是Android新手,我認爲我的想法很有幫助。其他2個答案也提出了類似我的答案。 – 2012-07-10 18:48:49