我正在尝试更改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;
}
答案 0 :(得分: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" />
drawable文件夹中的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>
drawable文件夹中的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>
我不确定它是否与上面的颜色相同。但工作原理是一样的
答案 1 :(得分: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>
遵循Code 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;
}