更改android Array Adapter中的列表项颜色

时间:2015-08-09 07:19:53

标签: android listview android-arrayadapter

我正在开发一个带有使用Fragments的菜单的简单WebView应用程序,一切正常,但ListView上的文本是黑色的,我不知道如何改变它。

我想知道如何更改列表视图的文本颜色......这是我的代码:

package com.lfcchile;


private ActionBarDrawerToggle actionBarDrawerToggle;
private ActionBar actionBar;
private DrawerLayout drawerLayout;
private ListView navList;
private FragmentTransaction fragmentTransaction;
private FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setIcon(R.mipmap.ic_launcher);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
    navList = (ListView) findViewById(R.id.navList);
    ArrayList<String> navArray = new ArrayList<String>();
    navArray.add("Inicio");
    navArray.add("Destacado");
    navArray.add("Tabla PL");
    navArray.add("Próximo Partido");
    navArray.add("Fragment 4");
    navArray.add("Fragment 5");
    navList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, navArray);
    navList.setAdapter(adapter);
    navList.setOnItemClickListener(this);
    actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.opendrawer, R.string.closedrawer);
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    //ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);
    fragmentManager = getSupportFragmentManager();

    loadSelection(0);
}

private void loadSelection(int i){
    navList.setItemChecked(i, true);

    switch (i){
        case 0:
            HomeFragment homeFragment = new HomeFragment();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragmentholder,homeFragment);
            fragmentTransaction.commit();
            break;

        case 1:
            MyFragment1 myFragment1 = new MyFragment1();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragmentholder,myFragment1);
            fragmentTransaction.commit();
            break;

        case 2:
            MyFragment2 myFragment2 = new MyFragment2();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragmentholder,myFragment2);
            fragmentTransaction.commit();
            break;

        case 3:
            MyFragment3 myFragment3 = new MyFragment3();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragmentholder,myFragment3);
            fragmentTransaction.commit();
            break;

        case 4:

            break;

        case 5:

            break;

    }

}

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    actionBarDrawerToggle.syncState();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    } else if (id == android.R.id.home) {
        if (drawerLayout.isDrawerOpen(navList)) {
            drawerLayout.closeDrawer(navList);
        } else {
            drawerLayout.openDrawer(navList);
        }
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
    loadSelection(position);

    drawerLayout.closeDrawer(navList);
}

这是我的XML

<android.support.v4.widget.DrawerLayout 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:id="@+id/drawerlayout"
>

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

</FrameLayout>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/navList"
    android:divider="@null"
    android:layout_gravity="start"
    android:backgroundTint="#8a0000"
    android:background="#8a0000" />

默认文字颜色是黑色但我想将其更改为白色,我该怎么办? 提前致谢

3 个答案:

答案 0 :(得分:2)

另一种最简单的方法是使用textSize,textStyle,color等创建一个包含所需textview的布局文件,然后将其与ArrayAdapter一起使用。

e.g。 mytextview.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv"
    android:textColor="@color/font_content"
    android:padding="5sp"
    android:layout_width="fill_parent"
    android:background="@drawable/rectgrad"
    android:singleLine="true"
    android:gravity="center"
    android:layout_height="fill_parent"/>

然后像往常一样将它与ArrayAdapter一起使用,如

private ListView navList;
navList = (ListView) findViewById(R.id.navList);
ArrayList<String> navArray = new ArrayList<String>();
    navArray.add("Inicio");
    navArray.add("Destacado");
    navArray.add("Tabla PL");
    navArray.add("Próximo Partido");
    navArray.add("Fragment 4");
    navArray.add("Fragment 5");
ArrayAdapter<String> ad = new ArrayAdapter<String>(context,R.layout.mytextview,navArray);
    navList.setAdapter(ad);

答案 1 :(得分:0)

不要使用android.R.layout.simple_list_item_activated_1作为ArrayAdapter内部行的模板,而是创建自己的行xml文件并在其中设置文字颜色:

<LinearLayout
    android:width="match_parent"
    android:height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="row_text_view"
        android:textColor="#00ff00"
        android:width="wrap_content"
        android:height="wrap_Content" />

</LinearLayout>

答案 2 :(得分:0)

你不要用系统提供的XML布局文件,你应该自己写一个XML布局,然后去引用它,而你就可以直接在你写的布局里面任意去修改文字的颜色。

不要使用xml布局文件系统,你应该编写自己的xml布局,然后引用它,但你可以直接在你的布局里写一下来修改任何文字的颜色。