2014-06-14 23 views
1

我有一個包含文本視圖,按鈕,微調和列表視圖一個活動
我的主XML文件包含:
如何更改FONTCOLOR和我的ListView的字體類型

<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="#ffff00" 
tools:context="com.example.taxitabriz.MainActivity" 
tools:ignore="MergeRootFrame" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="8dp" 
    android:text="@string/app_name" 
    android:textSize="35dp" 
    android:textStyle="bold" 
    android:textColor="#996600" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:id="@+id/linearlayout1" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textColor="#996600" 
     android:text="@string/exit" 
     android:background="@drawable/backbutton" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textColor="#996600" 
     android:text="@string/about" 
     android:background="@drawable/backbutton" /> 

</LinearLayout> 

<Spinner 
    android:id="@+id/spinner1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@+id/textView1" 
    android:layout_marginTop="19dp" /> 

<ListView 
    android:id="@+id/listView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/spinner1" 
    android:layout_above="@+id/linearlayout1" 
    android:layout_alignParentRight="true" 
    android:layout_weight="49.94" > 
</ListView> 


和我的java代碼部分是在這裏:

ArrayAdapter<String> ard=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1); 
    sp.setAdapter(ard); 
    lv1=(ListView) findViewById(R.id.listView1); 
    ArrayAdapter<String> ard1=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1); 
    lv1.setAdapter(ard1); 

,但我不能改變FON t列表視圖或微調器中的顏色或字體類型。我該怎麼做?

回答

2

你需要創建一個CustomListAdapter。

private class CustomListAdapter extends ArrayAdapter { 

    private Context mContext; 
    private int id; 
    private List <String>items ; 

    public CustomListAdapter(Context context, int textViewResourceId , List<String> list) 
    { 
     super(context, textViewResourceId, list);   
     mContext = context; 
     id = textViewResourceId; 
     items = list ; 
    } 

    @Override 
    public View getView(int position, View v, ViewGroup parent) 
    { 
     View mView = v ; 
     if(mView == null){ 
      LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      mView = vi.inflate(id, null); 
     } 

     TextView text = (TextView) mView.findViewById(R.id.textView); 

     if(items.get(position) != null) 
     { 
      text.setTextColor(Color.WHITE); 
      text.setText(items.get(position)); 
      text.setBackgroundColor(Color.RED); 
      int color = Color.argb(200, 255, 64, 64); 
       text.setBackgroundColor(color); 

     } 

     return mView; 
    } 

} 

列表項看起來像這樣(custom_list.xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/textView" 
    android:textSize="20px" android:paddingTop="10dip" android:paddingBottom="10dip"/> 
</LinearLayout> 

使用TextView的API對你的文字裝點自己的喜好

,你將使用它像這樣

listAdapter = new CustomListAdapter(YourActivity.this , R.layout.custom_list , mList); 
mListView.setAdapter(listAdapter); 
+0

感謝您的回答。在這種方法中,我如何使用OnClickListener方法來管理選定的項目? –

1

如果你真的想用機器人簡單的列表佈局,we see that they declare their textview's identifier as such

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    style="?android:attr/dropDownItemStyle" 
    android:textAppearance="?android:attr/textAppearanceLargeInverse" 
    android:singleLine="true" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:ellipsize="marquee" /> 

因此,只要您的自定義適配器,膨脹他們的佈局&發現,文本視圖的ID。類似以下內容:

public final class CustomAdapter extends ArrayAdapter<String> { 

    private Context context; 
    ViewHolder holder; 
    private ArrayList<String> messages; 


    public CustomAdapter(Context context, ArrayList<String> data) { 

     this.context = context; 
     this.messages = data; 
     Log.d("ChatAdapter", "called constructor"); 

    } 

    public int getCount() { 
     return messages.size(); 
    } 


    public View getView(int position, View convertView, ViewGroup viewGroup) { 

     if (convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) context 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.simple_dropdown_item_1line, null); 

      holder = new ViewHolder(); 
      holder.message = (TextView) convertView 
        .findViewById(R.id.text1); 

      convertView.setTag(holder); 

     } else { 
      holder = (ViewHolder) convertView.getTag(); 

     } 

     holder.message.setText(data.get(position)); 
     holder.message.setTextColor(Color.BLUE); 

     // don't do this, remember the face variable once just showing explicitly for u 
     Typeface face=Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf"); 

     holder.message.setTypeface(face); 
     return convertView; 
    } 

    public static class ViewHolder { 
     public TextView message; 
    } 
} 

編輯:以下是您將如何在自己的活動中使用自定義陣列適配器的方法。

// I'm not sure if your sp or lv1 wants the adapter, but 
// whatever you require your list view's data to be just 
// set the custom adapter to it 
CustomAdapter<String> myAdapter = new ArrayAdapter<String>(this, s1); 
lv1=(ListView) findViewById(R.id.listView1) 
lv1.setAdapter(myAdapter); 
+0

感謝您的回答,但我如何在我的mainactivity.java類中使用這個類?以及我在哪裏創建您在上面寫入的xml文件? –

+0

而不是實例化一個ArrayAdapter 使用上面的這個自定義適配器。 'CustomAdapter adapter = new CustomAdapter(getBaseContext(),s1); mListView.setAdapter(adapter);' –

+0

您不必創建xml文件,因爲它是android的xml文件。它已經提供給你。我只是引用它來向你展示如何更好地控制文本。我將編輯答案以顯示如何在活動中使用它。 – Ryan

0

你可以這樣做,在XML中添加ListView項,在res/layout/list_item1.xml

<?xml version="1.0" encoding="utf-8"?> 

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 

    android:id="@+id/text1" 

    android:layout_width="fill_parent" 

    android:layout_height="wrap_content" 

    android:textAppearance="?android:attr/textAppearanceLarge" 

    android:textColor="#0000ff" 



    android:testStyle="italic" 

    android:gravity="center_vertical" 

    android:paddingLeft="6dip" 

    android:minHeight="?android:attr/listPreferredItemHeight" 

/> 

然後,

ArrayAdapter<String> ard=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1); 
sp.setAdapter(ard); 
lv1=(ListView) findViewById(R.id.listView1); 
ArrayAdapter<String> ard1=new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line,s1);// **change R.layout.list_item1** 
lv1.setAdapter(ard1); 
+0

謝謝you.it的作品!但你能告訴我如何改變這個文本視圖的字體? @vic_chen –

0

您必須將字體(您要添加)的.ttf文件放在asstes/fonts/文件夾並在onCreate方法中編寫代碼,如下所示。我已經puted Chalkduster.ttfasstes /字體/文件夾

TextView txttest = (TextView) findViewById(R.id.txttest); 
Typeface custom_font = Typeface.createFromAsset(getAssets(), 
      "fonts/Chalkduster.ttf"); 
txttest.setTypeface(custom_font); 
+0

我知道,但在這種情況下,它不起作用。可能原因是文本視圖使用佈局 –

+0

您已添加自定義字體的代碼?必須添加此代碼在適配器的getview()方法 – MFP

+0

謝謝你的答案。我想添加自定義字體vic_chen的代碼在這個Question.how回答我可以這樣做嗎? –

0

您可以處理編寫代碼所選項目的點擊,就像下面

ListView lvNews = (ListView) findViewById(R.id.lvNews); 
lvNews.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, 
       int position, long arg3) { 
      // Write your code here  
      //int newsId = newsArray.get(position).getId(); 
      //Intent i = new Intent(NewsListActivity.this, 
      //  NewsDetailActivity.class); 
      //i.putExtra(Constants.NEWS_ID, newsId); 
      //i.putExtra("isFromNotification", false); 
      //startActivity(i); 

     } 

    });