2014-09-04 4 views
0

В моей основной деятельности есть ListView, но на некоторых устройствах, таких как Galaxy s3, элемент черный, но на моем S4 он отображается нормально, и я вижу текст. Я пробовал андроид: background = "@ android: color/transparent, но он по-прежнему черный, а также android: cacheColorHint =" @ android: color/transparent "dosen't help. Я надеюсь, что кто-то может помочь.ListView черный на некоторых устройствах

ГлавнаяActivity:

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

ответ

0

я был один и тот же вопрос, что вы описываете, это выглядит, как у вас также есть настраиваемое представление для ДИСПЛЕЯ ying элементы списка - так что это может сработать и для вас!

Я переопределил метод getView в своем «CustomAdapter», классе, который расширяет ArrayAdapter. И в методе getView в этом классе я устанавливаю все элементы, которые не выбраны для drawable - и важно, чтобы drawable имел сплошной цвет - например:

(Это 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; 
    } 
} 

Черный ListView показывал только тогда, когда я запустил приложение на Galaxy S2. Это было прозрачно, как я хотел на моем S4 Mini - надеюсь, это может помочь вам в некоторой степени!

Смежные вопросы