2015-08-20 18 views
0

我对Android相当陌生。我正在寻找更改列表的文本颜色,嵌套在导航抽屉中。我有一个带有字符串数组和5个项目的资源文件。然后我使用ArrayAdapter填充导航抽屉,但我创建了“菜单”作为ListView。 ListView没有我可以设置的textColor属性。我怎样才能改变ListView的文字颜色?更改由ArrayAdapter生成的列表的文本颜色

MainActivity.java

public class MainActivity extends AppCompatActivity { 

    private String[] menuItemsForDrawer; 
    private DrawerLayout drawerLayout; 
    private ListView gpsJobcardDrawerList; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     menuItemsForDrawer = getResources().getStringArray(R.array.gps_jobcard_menu_items); 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     gpsJobcardDrawerList = (ListView) findViewById(R.id.left_drawer); 

     gpsJobcardDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuItemsForDrawer)); 
    } 
} 

activity_main.xml中

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!-- Main Content View (Must be first child in the DrawerLayout --> 
    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@drawable/dark_grey_color"/> 
    <!-- Navigation Drawer --> 
    <ListView 
     android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="@drawable/orange_color" /> 
</android.support.v4.widget.DrawerLayout> 

gps_jobcard_menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string-array name="gps_jobcard_menu_items"> 
     <item>Item 1</item> 
     <item>Item 2</item> 
     <item>Item 3</item> 
     <item>Item 4</item> 
     <item>Item 5</item> 
    </string-array> 
</resources> 
+1

您需要创建一个自定义资源。 – Rohit5k2

回答

0

当前资源android.R.layout.simple_list_item_1您正在使用的是Android资源。如果你需要改变它的外观和感觉,那么你应该创建自己的资源并使用它。这是我们的做法。

创建一个XML布局说navigation_item.xml这样

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#25383C" <!-- any color you need --> 
/> 

,改变

gpsJobcardDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menuItemsForDrawer)); 

gpsJobcardDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.navigation_item, menuItemsForDrawer)); 
+0

谢谢,它的工作很好。感谢帮助 –

+0

@OwenNel:太棒了!你可以接受这个答案。 :d – Rohit5k2

0

不使用android.R.layout.simple_list_item_1使自己的自定义布局

layout_simple_list_item.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="wrap_content" 
> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="8.0dip" 
    android:text="Text" 
    android:textColor="" 
    android:textSize="20.0sp" /> 

</LinearLayout> 

代码

gpsJobcardDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.layout_simple_list_item, menuItemsForDrawer)); 
+0

如果您在“ArrayAdapter”内部使用的资源中添加了除“TextView”之外的任何节点,您将面临错误“ArrayAdapter需要资源ID为TextView',除非您创建自定义适配器。 – Rohit5k2

0
android.R.layout.simple_list_item_1 

这是一款Android系统的布局,我想你可以通过提取由反射TextView的编辑文本的颜色和然后设置相同的参数。