如何更改列表视图的文本颜色

时间:2013-12-10 20:15:26

标签: java android xml

好的,所以使用textView的技巧不适用于此。 所以我知道这必须简单......对吧? 因为我可能会帮助我以后添加图片。

<?xml version="1.0" encoding="utf-8"?>
<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/logo"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/textView1"
        android:paddingLeft="8dp"
        android:paddingRight="8dp" >

    </ListView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/welcome"
        android:textColor="#b70000"
        android:textSize="16sp" />

</RelativeLayout>

我用它来调用它并添加到java代码中的listview。

package com.example.boonehallfrightnightsapp;


import android.os.Bundle;
import android.view.Menu;
import android.app.ListActivity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ListActivity 
{

    static final String[] CHOICES = new String[]
            {
                "Haunted House",
                "Amy's Nightmare",
                "Zombie Town",
                "Haunted Hayride",
                "Quit"
            };


    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fn_main);

         //found this part on an example
         //Set up ArrayAdaptor for the options
        setListAdapter(new ArrayAdapter<String>
            (this, android.R.layout.simple_list_item_1, CHOICES));
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        getListView().setTextFilterEnabled(true);

      //part of example  
      //Set up the listener for user clicks on the list
        setListClickListener();

        //this toast is for when it opens
        Toast.makeText(this, "I see your fear...", Toast.LENGTH_SHORT).show();
    }


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


     private void setListClickListener()
        {
            //Set up the click listener for the options
            getListView().setOnItemClickListener
            (
                new OnItemClickListener()
                {
                    //@Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
                    {
                        switch(arg2)
                        {
                            case 0: launchHousePage();
                                    break;
                            case 1: launchNightmarePage();
                                    break;
                            case 2: launchZombiePage();
                                    break;
                            case 3: launchHayridePage();
                                    break;
                            case 4: finish();
                                    break;
                            default: break;
                        }
                    }
                }//END OnItemClickListener
            );//END setOnItemClickListener
        }//END setListClickListener

     //goes to haunted house
     protected void launchHousePage()
        {
            //Set up Intent
            Intent launchHouse = new Intent(this, HauntedHouseList.class);
            startActivity(launchHouse);

        }//END launchHousePage

     //goes to Amy's Nightmare
     protected void launchNightmarePage()
        {
            //Set up Intent
            Intent launchnightmare = new Intent(this, NightmareList.class);
            startActivity(launchnightmare);

        }//END launchNightmarePage

     //goes to Amy's Nightmare
     protected void launchZombiePage()
        {
            //Set up Intent
            Intent launchzombies = new Intent(this, ZombieTownList.class);
            startActivity(launchzombies);

        }//END launchZombiePage 

     //goes to haunted house
     protected void launchHayridePage()
        {
            //Set up Intent
            Intent launchhayride = new Intent(this, HauntedHayrideList.class);
            startActivity(launchhayride);

        }//END launchHayridePage
}

1 个答案:

答案 0 :(得分:3)

您已使用项目布局android.R.layout.simple_list_item_1指定了内置适配器(ArrayAdapter)。

如果您想要自定义布局,可以将Android SDK中的simple_list_item_1.xml布局(在platforms / android-18 / data / res / layout文件夹中查找)复制到项目中并进行修改。例如,您将其命名为my_simple_list_item_1.xml。

然后修改你的代码以使用你的布局,而不是android.R.layout.simple_list_item_1:

setListAdapter(new ArrayAdapter<String>(this, R.layout.my_simple_list_item_1, CHOICES));

您会看到Androids simple_list_item_1布局只是一个 TextView ,您可以将textColor属性添加到它并根据自己的喜好进行修改。