有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    安卓:background="@drawable/logo"
    安卓:paddingBottom="@dimen/activity_vertical_margin"
    安卓:paddingLeft="@dimen/activity_horizontal_margin"
    安卓:paddingRight="@dimen/activity_horizontal_margin"
    安卓:paddingTop="@dimen/activity_vertical_margin"
    >

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

    </ListView>

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

</RelativeLayout>

我使用它来调用它,并将其添加到java代码中的listview中

package com.example.boonehallfrightnightsapp;


import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.app.ListActivity;
import 安卓.content.Intent;
import 安卓.view.View;
import 安卓.widget.AdapterView;
import 安卓.widget.AdapterView.OnItemClickListener;
import 安卓.widget.ArrayAdapter;
import 安卓.widget.ListView;
import 安卓.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, 安卓.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) 个答案

  1. # 1 楼答案

    您已经使用item layout android指定了一个内置适配器(ArrayAdapter)。R.布局。简单列表项目1

    如果需要自定义布局,可以复制简单列表项1。从Android SDK(查看platforms/Android-18/data/res/layout文件夹)到项目中的xml布局,并对其进行修改。例如,你称之为我的简单清单项目1。xml

    然后修改代码以使用布局,而不是android。R.布局。简单列表项目1:

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

    你会看到Androids simple_list_item_1布局只是一个文本视图,你可以添加textColor属性并根据自己的喜好进行修改