2015-12-26 2 views
-1

Как изменить цвет текста ListView (в макете ящика) в моем коде? В моем списке кодов есть другой формат. Я попробовал различные другие коды доступны в StackOverflow, но никто, казалось, не работать для меняКак изменить цвет текста внутри ListView?

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<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" 
    tools:context=".MainActivity"> 

    <LinearLayout 
     android:id="@+id/lltoolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:orientation="vertical"> 

     <include 
      android:id="@+id/app_bar" 
      layout="@layout/app_bar" /> 

    </LinearLayout> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/lltoolbar" 
     android:text="@string/hello_world" /> 


</RelativeLayout> 

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawerLayout" 
    android:layout_marginTop="55dp" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <FrameLayout 
     android:id="@+id/mainContent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

    </FrameLayout> 

    <ListView 
     android:id="@+id/drawerList" 
     android:entries="@array/abcd" 
     android:background="#FFFFFF" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="left" /> 

</android.support.v4.widget.DrawerLayout> 


private Toolbar toolbar; 
TextView textView; 
ImageView iv; 
DrawerLayout drawerLayout; 
private ListView listView; 
private String[] names; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    toolbar = (Toolbar) findViewById(R.id.app_bar); 
    textView = (TextView) findViewById(R.id.toolbar_title); 
    iv = (ImageView) findViewById(R.id.ivClick); 
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);  

    listView = (ListView) findViewById(R.id.drawerList); 
    names = getResources().getStringArray(R.array.abcd); 
    listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names)); 
    listView.setOnItemClickListener(this); 

} 

ответ

1

Вы должны создать xml-макет для каждой строки вашего списка, затем создать класс, расширяющий BaseAdapter, и внутри getview раздувать xml-макет, а затем установить цвет для текстового представления.

0

Выполните следующие действия:

  1. Создание макета XML, который будет будет отображаться в каждой строке вашего списка. (В настоящее время вы используете раскладку по умолчанию android.R.layout.simple_list-Item_1) что-то вроде этого:
    my_listrow_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="sample text" 
     android:text_color="#006699" 
     android:id="@+id/list_item"/> 
</LinearLayout> 

2. В коде активности, изменить список адаптер код этого:

listView.setAdapter(new ArrayAdapter<>(this, R.layout.my_listrow_layout, 
R.id.list_item, names)); 

my_listrow_layout ваш заказ макет для ListView, R.id.list_item это идентификатор вашего TextView в ListView ла Вы т. Чтобы изменить цвет текста, добавьте значение android: text_color = "ваш желаемый цвет" в только что созданном макете. (Я установил его на # 006699, который имеет синий цвет).

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