Android 某些设备上的ListView为黑色
在我的主要活动是一个列表视图,但在Galaxy s3等设备上,该项目是黑色的,但在我的S4上,它显示正常,我可以看到文本。我尝试了android:background=“@android:color/transparent”,但它仍然是黑色的,而且android:cacheColorHint=“@android:color/transparent”也没有帮助。我希望有人能帮助我 主要活动:Android 某些设备上的ListView为黑色,android,xml,listview,Android,Xml,Listview,在我的主要活动是一个列表视图,但在Galaxy s3等设备上,该项目是黑色的,但在我的S4上,它显示正常,我可以看到文本。我尝试了android:background=“@android:color/transparent”,但它仍然是黑色的,而且android:cacheColorHint=“@android:color/transparent”也没有帮助。我希望有人能帮助我 主要活动: <RelativeLayout xmlns:android="http://schemas.an
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:cacheColorHint="@android:color/transparent"
android:background="@android:color/transparent" >
</ListView>
ListView仅显示第一个标题,不显示内容textview:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/list_item_border"
android:cacheColorHint="@android:color/transparent" >
<!-- <style android:name="Divider">
<item android:name="android:layout_height">100dp</item>
<item android:name="android:background">?android:attr/listDivider</item>
</style> -->
<TextView
android:id="@+id/titleItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:paddingBottom="10dp"
android:textColor="#000000"
android:cacheColorHint="@android:color/transparent"
android:background="@android:color/transparent"
android:textSize="25dp" />
我遇到了与您描述的问题相同的问题。看起来您还有一个用于显示listview项目的自定义视图-因此这可能也适用于您 我已经在我的“CustomAdapter”类中重写了getView方法,该类扩展了ArrayAdapter。在该类中的getView方法中,我将所有未选中的项设置为可绘制项,重要的是,可绘制项具有纯色,如下所示: (这是R.drawable.item_默认值)
这样做解决了我的问题。下面是扩展类的代码:
public class CustomAdapter extends ArrayAdapter<String> {
private ArrayList<String> items;
private LayoutInflater mInflater;
private int viewResourceId;
private int selectedPosition = -1;
private int selectedTextView;
private Context myContext;
public CustomAdapter(Activity activity,int resourceId, int textViewId,
ArrayList<String> list, Context context) {
super(activity,resourceId,textViewId, list);
// Sets the layout inflater
mInflater = (LayoutInflater)activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Set a copy of the layout to inflate
viewResourceId = resourceId;
selectedTextView = textViewId;
myContext = context;
// Set a copy of the list
items = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layout = (LinearLayout) convertView;
if (layout == null) {
layout = (LinearLayout)mInflater.inflate(viewResourceId, null);
}
TextView tv = (TextView) layout.findViewById(selectedTextView);
tv.setText(items.get(position));
// Change the background color
if (position==selectedPosition){
tv.setBackground(myContext.getResources().getDrawable(R.drawable.item_pressed));
}
else {
tv.setBackground(myContext.getResources().getDrawable(R.drawable.item_default));
}
return layout;
}
public void setSelected(int position) {
selectedPosition = position;
}
}
公共类CustomAdapter扩展了ArrayAdapter{
私有ArrayList项;
私人停车场;
私有int-viewResourceId;
private int selectedPosition=-1;
私有int-selectedTextView;
私人语境;
公共CustomAdapter(活动活动、int resourceId、int textViewId、,
ArrayList列表,上下文){
超级(活动、资源ID、文本视图ID、列表);
//设置布局充气器
mInflater=(LayoutInflater)活动
.getSystemService(上下文布局\充气机\服务);
//将布局的副本设置为充气
viewResourceId=resourceId;
selectedTextView=textViewId;
myContext=上下文;
//设置列表的副本
项目=列表;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LinearLayout布局=(LinearLayout)转换视图;
if(布局==null){
布局=(LinearLayout)mInflater.inflate(viewResourceId,null);
}
TextView tv=(TextView)layout.findViewById(selectedTextView);
tv.setText(items.get(position));
//更改背景色
如果(位置==selectedPosition){
tv.setBackground(myContext.getResources().getDrawable(R.drawable.item_pressed));
}
否则{
setBackground(myContext.getResources().getDrawable(R.drawable.item_default));
}
返回布局;
}
公共职位(国际职位){
selectedPosition=位置;
}
}
当我在Galaxy S2上运行应用程序时,黑色列表视图才显示出来。它是透明的,就像我在S4 Mini上想要的一样-希望这能在某种程度上帮助你
public class CustomAdapter extends ArrayAdapter<String> {
private ArrayList<String> items;
private LayoutInflater mInflater;
private int viewResourceId;
private int selectedPosition = -1;
private int selectedTextView;
private Context myContext;
public CustomAdapter(Activity activity,int resourceId, int textViewId,
ArrayList<String> list, Context context) {
super(activity,resourceId,textViewId, list);
// Sets the layout inflater
mInflater = (LayoutInflater)activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Set a copy of the layout to inflate
viewResourceId = resourceId;
selectedTextView = textViewId;
myContext = context;
// Set a copy of the list
items = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout layout = (LinearLayout) convertView;
if (layout == null) {
layout = (LinearLayout)mInflater.inflate(viewResourceId, null);
}
TextView tv = (TextView) layout.findViewById(selectedTextView);
tv.setText(items.get(position));
// Change the background color
if (position==selectedPosition){
tv.setBackground(myContext.getResources().getDrawable(R.drawable.item_pressed));
}
else {
tv.setBackground(myContext.getResources().getDrawable(R.drawable.item_default));
}
return layout;
}
public void setSelected(int position) {
selectedPosition = position;
}
}