2012-10-16 72 views
3

我開發了一個例子。我怎樣才能讓android spinner提示樣式

在這裏,我必須設計一個微調提示符。請幫幫我。我如何設計它?

這是我string.xml代碼:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="hello">Hello World, CustomizedListView!</string> 
<string name="app_name">CustomizedListView</string> 
<string name="status_prompt">Choose a Status</string> 
</resources> 

此代碼對我main.xml中使用:

<Spinner android:id="@+id/spinner1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_x="106dp" 
    android:layout_y="100dp" 
    android:prompt="@string/status_prompt" 
    android:background="@drawable/btn_dropdown"/> 

在這裏,我想改變背景顏色和textSizetextStyle在下面的圖像。我該如何改變這一點。

spinner prompt

編輯: 我已經添加下面的代碼在styles.xml

<style name="spinner_style">       
<item name="android:layout_width">fill_parent</item> 
<item name="android:layout_height">wrap_content</item> 
<item name="android:textColor">#040404</item> 
<item name="android:typeface">monospace</item> 
<item name="android:background">#FFFFFF</item> 
</style> 

還添加以下行上main.xml中爲旋轉器:

style="@style/spinner_style" 

但它也沒有爲我工作。我如何設計微調框提示信息。

編輯:

這是我ArrayAdaper Java代碼:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_item, list); 
    //set the view for the Drop down list 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    //set the ArrayAdapter to the spinner 
    spinner.setAdapter(adapter); 
    adapter.notifyDataSetChanged(); 
    //attach the listener to the spinner 
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); 

    } 

我如何設置提示風格在這裏。

回答

2

您需要爲微調器創建自己的適配器,其範圍從ArrayAdapter延伸。當佈局系統指示您已經在圖片中顯示的視圖需要繪製時,將調用getView()函數。您可以覆蓋它並添加TEXTSIZE,文字樣式等

更新:一個粗略的代碼示例將是:

private class MySpinnerAdapter extends ArrayAdapter<String> { 
     public MySpinnerAdapter(Context context, int resource, 
       int textViewResourceId, List<String> objects) { 
      super(context, resource, textViewResourceId, objects); 
     } 

    public View getView(int position, View convertView, 
      ViewGroup parent) { 
     View v = super.getView(position, convertView, parent); 
     // apply the style and sizes etc to the Text view from this view v 
     // like ((TextView)v).setTextSize(...) etc 
     return v; 
    } 

    public View getDropDownView(int position, View convertView, 
      ViewGroup parent) { 
     // this is for each of the drop down resource that is created. 
      // you can style these things too 
     } 
} 

以此爲適配器爲您的微調:

mySpinner.setAdapter(new MySpinnerAdapter(ActivityName.this, 
        R.layout.spinner_item, 
        R.id.spinner_item_TextView, 
        listOfItemsInSpinner)); 
+2

上面的代碼只改變微調項目的樣式。我希望改變微調控件的提示樣式。 – user1676640

3

爲了要改變提示,您需要使用與@ aswin-kumar建議的類似方法,但是對於第一個元素的下拉列表使用單獨的樣式:

首先,擴展ArrayAdapter並稱之爲CustomArrayAdapter: package com.example.project;

package com.example.project; 

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

import java.util.List; 


// I extend this using ArrayAdapter<String>, but you can use anything as the type 
// parameter, or parameterize it as necessary. 
public class CustomArrayAdapter extends ArrayAdapter<String> { 

    private String title; 

    public CustomArrayAdapter(Context aContext, int aTextViewResource, List<String> aOptions, String title) { 
     super(aContext, aTextViewResource, aOptions); 
     this.title = title; 
    } 

    @Override 
    public View getDropDownView(int aPosition, View aConvertView, ViewGroup aParent) { 

     LinearLayout v = null; 

     if (aPosition == 0) { 
      LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = (LinearLayout) inflater.inflate(R.layout.dropdown_header, aParent, false); 
      TextView tv = (TextView) v.findViewById(R.id.dropdown_item); 
      tv.setText(getItem(aPosition)); 
     } else { 
      LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      v = (LinearLayout) inflater.inflate(R.layout.dropdown_item, aParent, false); 
      TextView tv = (TextView) v.findViewById(R.id.dropdown_item); 
      tv.setText(getItem(aPosition)); 
      tv.setHeight((int) (tv.getTextSize() * 2)); 
     } 


     return v; 
    } 

    @Override 
    public int getCount() { 
     return super.getCount() + 1; 
    } 

    @Override 
    public String getItem(int position) { 
     if (position == 0) { 
      return title; 
     } 
     return super.getItem(position - 1); 
    } 

    @Override 
    public boolean isEnabled(int position) { 
     return position != 0; 
    } 
} 

現在,請您dropdown_header.xml以下佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/dropdown_item" 
     android:textColor="@android:color/black" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="16dp" 
     android:paddingBottom="16dp" 
     android:paddingLeft="16dp" 
     android:gravity="left|center_vertical" 
     android:textSize="9pt" /> 

    <!-- This adds a black separator line between the title and the items. You can remove 
     if you want --> 
    <LinearLayout 
     android:id="@+id/separator" 
     android:layout_height="4dp" 
     android:layout_width="match_parent" 
     android:background="@android:color/black" 
     android:orientation="vertical"/> 
    </LinearLayout> 

表單視圖佈局文件(form_view_layout。XML):

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/spinner_dialog_root" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textSize="9pt" /> 

的微調佈局文件(spinner_layout.xml):

<Spinner 
android:id="@+id/spinner" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentTop="true" 
android:background="@android:color/transparent"/> 

現在,附加新的適配器與Spinner

CustomArrayAdapter adapter = new CustomArrayAdapter(aActivity, R.layout.form_view_layout, aModel.getEnumerableOptions(), aModel.getTitle()); 
View parentView = aActivity.getLayoutInflater().inflate(R.layout.spinner_layout, aParent, false); 

spinner = (Spinner) parentView.findViewById(R.id.spinner); 
spinner.setAdapter(adapter); 

最後,添加你想要的字符串顯示到您的微調適配器,adapter,並設計適當的文件,你應該很好去。