2013-03-24 236 views
0

我試圖更改ListView中的顏色,因爲我想使用白色背景。不用觸摸ListView就不顯示文本。觸摸時,背景會切換爲黑色,而文本是可見的,因爲文本是白色的。 如何更改Textcolour並避免背景在接觸時更改顏色? 這裏是我的代碼:在ListView中更改顏色

 public class LayoutNight extends Fragment { 

     ListView myList; 

     String[] listContent = { 

       "Mo", 
       "Di", 
       "Mi", 
       "Do", 
       "Fr", 
       "Sa", 
       "So" 

     }; 

     public static Fragment newInstance(Context context) { 
      LayoutNight f = new LayoutNight(); 

      return f; 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
      ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_night, null);  

      myList = (ListView)root.findViewById(R.id.list); 

      ArrayAdapter<String> adapter 

      = new ArrayAdapter<String>(getActivity(), 

        android.R.layout.simple_list_item_multiple_choice, 

       listContent); 



      myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

      myList.setAdapter(adapter); 
// That was a try, but it crashed ... 

    //  TextView myListText = (TextView)root.findViewById(R.id.list); 
    //  myListText.setTextColor(color.black); 

      return root; 
     } 

    } 

編輯

我做Raghunandan的建議,但背景沒有改變。 這是不變的截圖

untouched

這是屏幕的滾動(不幸的是黑色scrolling

而且這是在屏幕的選擇選項 selecting option

編輯 現在它的工作方式如下,而不需要在滾動時更改背景(感謝Pragnani和Raghunandan),但它不再可點擊。我怎樣才能讓它可點擊?

<ListView 
android:id="@+id/list" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:background="@drawable/listbkg" 
android:drawSelectorOnTop="true" 
android:cacheColorHint="@android:color/transparent"> 
</ListView> 

list_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="5px" 
     android:paddingBottom="5px" 
     android:textSize="35sp" 
     android:textColor="#000000" 
     android:layout_gravity="center" 
     android:maxHeight="75px"/> 

代碼:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_night, null);  

    myList = (ListView)root.findViewById(R.id.list); 

    ArrayAdapter<String> adapter 
    = new ArrayAdapter<String>(getActivity(),R.layout.list_row,listContent); 

    myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
    myList.setFocusable(true); 
    myList.setClickable(true); 
    myList.setAdapter(adapter); 

    return root; 
} 
+0

爲了達到這個目的,你必須製作你自己的listview適配器! – Pavlos 2013-03-24 19:02:03

+2

而不是此android.R.layout.simple_list_item_multiple_choice,使用您的自定義佈局只有一個文本視圖,並將其設置爲您的ArrayAdapter佈局資源。給你喜歡的屬性,如文字顏色,尺寸等 – Pragnani 2013-03-24 19:02:14

+1

但這不會改變顏色的變化,而觸摸。 你需要一個選擇器,http://stackoverflow.com/questions/3817776/problem-setting-listview-selector-color-when-pressed – fish 2013-03-24 19:04:03

回答

0

我不得不主題更改爲Theme.Light,對不起,我是初學者...

這是現在的ListView

<ListView 
android:id="@+id/list" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:background="@color/white" 
android:drawSelectorOnTop="true" 
android:cacheColorHint="@android:color/transparent"> 
</ListView> 

而繼代碼snippset

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.layout_night, null);  

    myList = (ListView)root.findViewById(R.id.list); 

    ArrayAdapter<String> adapter 
    = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_multiple_choice,listContent); 

    myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 

    View header = (View)inflater.inflate(R.layout.listview_header_row, null); 

    myList.addHeaderView(header);   
    myList.setAdapter(adapter); 

    return root; 
} 
1

我建議你使用自定義的適配器。易於定製。

在您的getview中充氣自定義佈局。

http://developer.android.com/guide/topics/resources/drawable-resource.html。檢查狀態列表下的主題。

public View getView(final int arg0, View arg1, ViewGroup arg2) { 
    final ViewHolder vh; 
    vh= new ViewHolder(); 

    if(arg1==null) 
     { 
     arg1=mInflater.inflate(R.layout.customlayout, arg2,false); 
     } 
    else 
    { 
    arg1.setTag(vh); 
    } 

    return arg1; 
} 

customlayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal" 
android:background="@drawable/listbkg" //set layout background 
android:cacheColorHint="#000000" 
> 
// other ui elements. similarly you can set background for your other ui elements 
//android:background="@drawable/otherui" 
// define your own drawables when pressed and in normal state. 

</LinearLayout> 

listbkg.xml在抽拉夾

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" 
    android:drawable="@drawable/pressed" /> 
<item android:state_focused="false" 
    android:drawable="@drawable/normal" /> 

pressed.xml在抽拉夾

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FF1A47"/>  
<stroke android:width="3dp" 
     android:color="#0FECFF"/> 
<padding android:left="5dp" 
     android:top="5dp" 
     android:right="5dp" 
     android:bottom="5dp"/> 
<corners android:bottomRightRadius="7dp" 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
</shape> 

normal.xml在繪製文件夾

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#FFFFFF"/> //can change color 
<stroke android:width="3dp"   //border color 
     android:color="#0FECFF" /><!-- #330000FF #ffffffff --> 
<gradient   //add gradient depending on your need else you can remove this 
    android:startColor="#ffffffff" 
    android:endColor="#110000FF" 
    android:angle="90"/> 

<padding android:left="5dp" 
     android:top="5dp" 
     android:right="5dp" 
     android:bottom="5dp"/> 
<corners android:bottomRightRadius="7dp"//for rounded corners 
     android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" 
     android:topRightRadius="7dp"/> 
</shape> 

我不知道,如果它的顏色一樣的上方。但工作原理是一樣的

enter image description here

+0

@ user1390816嘗試以上。 – Raghunandan 2013-03-24 19:27:35

+0

謝謝我會試試! – user1390816 2013-03-24 19:33:24

+0

不幸的是背景顏色沒有改變。我在我的帖子的編輯中附加了截圖。 – user1390816 2013-03-27 19:04:27