【问题标题】:ListView is black on some DevicesListView 在某些设备上是黑色的
【发布时间】:2014-09-04 18:57:25
【问题描述】:

在我的 Main Activity 上是一个 ListView,但在 Galaxy s3 等某些设备上,该项目是黑色的,但在我的 S4 上它显示正常,我可以看到文本。我试过 android:background="@android:color/transparent 但它仍然是黑色的,也 android:cacheColorHint="@android:color/transparent" 没有帮助。我希望有人能帮忙。

主活动:

  <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" />

【问题讨论】:

    标签: android xml listview


    【解决方案1】:

    我遇到了与您描述的相同的问题。看起来您还有一个用于显示列表视图项目的自定义视图 - 所以这也可能对您有用!

    我已经覆盖了“CustomAdapter”中的 getView 方法,这是一个扩展 ArrayAdapter 的类。在该类的 getView 方法中,我将所有未选择的项目设置为可绘制对象 - 重要的是可绘制对象具有纯色 - 如下所示:

    (这是 R.drawable.item_default)

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle" >
    
        <solid android:color="@android:color/transparent" />
    
    </shape>
    

    这样做解决了我的问题。下面是我的扩展类的代码:

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

    只有当我在 Galaxy S2 上运行应用程序时,才会显示黑色列表视图。它是透明的,就像我在 S4 Mini 上想要的一样 - 希望这可以在一定程度上帮助你!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多