2013-12-10 76 views
-1

好,所以使用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 
} 
+0

通常您的ListView與一個適配器一起使用,您可以在其中爲列表項定義一個單獨的xml文件... –

+0

@ M.Bennett我通過添加我的Java代碼更新了我的問題。 –

回答

3

您已經指定了使用內置的適配器(ArrayAdapter)項目佈局android.R.layout.simple_list_item_1。

如果您需要自定義佈局,您可以將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)); 

你會看到,機器人會simple_list_item_1管理佈局只是一個TextView的,你可以添加textColor屬性,並根據自己的喜好進行修改。

相關問題