0

我正在尝试更改 ListView 中的颜色,因为我想使用白色背景。不触摸 ListView 它不显示任何文本。触摸时背景切换为黑色,并且文本可见,因为文本是白色的。如何更改 Textcolour 并避免触摸时背景改变其颜色?这是我的代码:

        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的建议,但背景没有改变。这是原封不动的截图

原封不动

这是滚动时的屏幕(不幸的是黑色滚动

这是选择选项的屏幕 选择选项

编辑 它现在就像在滚动时不改变背景一样工作(感谢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;
}
4

2 回答 2

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;
}

自定义布局.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>

我不确定它是否与上面的颜色相同。但是工作原理是一样的

在此处输入图像描述

于 2013-03-24T19:26:23.303 回答
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>

并遵循代码片段

    @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;
}
于 2013-03-30T14:13:41.633 回答