如何在android中更改自定义微调器的样式

时间:2013-05-16 12:15:45

标签: android android-spinner

我的观点是这样的, 我正在为微调器使用自定义适配器

我从这个链接设计了我的微调器 http://rimpv.blogspot.in/2013/05/bind-spinner-dropdown-in-android.html

@Override
public View getView(int position, View convertView, ViewGroup parent) {
            TextView v = new TextView(getApplicationContext());
            v.setTextColor(Color.BLACK);
            v.setText(data.get(position).name);
            return v;
        }

enter image description here

但我希望像enter image description here

一样展示

如何在自定义微调器中执行此操作?提前感谢您的帮助

5 个答案:

答案 0 :(得分:1)

编辑3

这个例子只是从头开始,现在没有IDE来测试它。

如果您不需要自定义适配器,则可以使用标准ArrayAdapter。然后为您的适配器设置dropDownViewResource。

public class Main extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    Spinner s = (Spinner) this.findViewById(R.id.spinner);

    ArrayList<String> list = new ArrayList<String>();
    list.add("Germany");
    list.add("USA");
    list.add("Nairobi");
    list.add("Japan");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    s.setAdapter(adapter);

    s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {

            Toast.makeText(getApplicationContext(),
                    "CLICKED:"+parent.getItemAtPosition(pos).toString(),
                    Toast.LENGTH_LONG).show();
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    });
}

但是如果你需要一个customAdapter,那么除了构建你自己的微调器项目布局之外别无他法:

构建您的main.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
    >
   <Spinner 
      android:drawSelectorOnTop="true"
      android:id="@+id/example_spinner" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"  > 
   </Spinner>
  </LinearLayout>

构建您的Spinner项xml: spinner_item.xml:

    <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/spinner_item_linear_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"
       android:orientation=”vertical”>


    <TextView        
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content "
      android:id="@+id/spinner_textView" >        
    </TextView>

    <RadioButton
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:id=”@+id/spinner_radio_button” >
   </RadioButton>

 </LinearLayout>

使用一些输入定义字符串数组,创建自定义适配器并将适配器设置为微调器。

    public class CustomSpinnerExample extends Activity {


  String []countries ={"Germany","USA","Nairobi","Japan"};

    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Spinner mySpinner = (Spinner)findViewById(R.id.example_spinner);
    mySpinner.setAdapter(new CustomAdapter(this, R.layout.spinner_item, countries));
   }


    public class CustomAdapter extends ArrayAdapter<String>
   {

        public CustomAdapter(Context context, int resourceId,
                    String[] objects) {
              super(context, resourceId, objects);
              // TODO Auto-generated constructor stub
        }
        @Override
     public View getDropDownView(int position, View convertView,ViewGroup parent) {
        return getCustomView(position, convertView, parent);
     }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater=getLayoutInflater();
        View row=inflater.inflate(R.layout.spinner_item, parent, false);
        TextView label=(TextView)row.findViewById(R.id.spinner_textView);
         label.setText(countries[position]);


        RadioButton radioButton    =(RadioButton)row.findViewById(R.id.spinner_radio_button);
       radioButton.setOnClickListener(new OnClickListener(){

   public void onClick(View v){

Toast.makeText(CustomAdapter.this,”CLICKED:”+label.getText(),Toast.LENGTH_LONG).show();

  }
    });

        return row;
        }

   }
}

答案 1 :(得分:1)

您可以使用源代码

创建新的“doubleline_spinner.xml”
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="false"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:ellipsize="none" 
android:textSize="15dp"

/>

并将其添加到您的微调器中

        ArrayAdapter<CharSequence> PolarityAAdapter = ArrayAdapter.createFromResource(this,
        R.array.polarity_arrays,android.R.layout.simple_spinner_item);
        PolarityAAdapter.setDropDownViewResource(R.layout.doubleline_spinner);
        PolarityAspinner.setAdapter(PolarityAAdapter);

答案 2 :(得分:0)

您可以看一下像this这样的简单教程。但我确定你不想这样做。

所以看看How to make a custom adapter with layout,教程是针对listView的,但它与spinner的操作相同。

您可以创建自己的布局,其中包含TextViewCheckBox之类的内容:

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@+id/linearLayout1″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >

<CheckBox
android:id=”@+DropDownList/checkbox”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />

<TextView
android:id=”@+DropDownList/SelectOption”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />

</LinearLayout>

并在您的函数中:

@Override
    public int getItemViewType(int position) {
        return R.layout.YourLayout;
    }

答案 3 :(得分:0)

Look at this tutorial

您可以更改微调器的背景图像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Spinner
    android:drawSelectorOnTop="true"
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/select">
    </Spinner>
</LinearLayout>

添加:

android:src="@drawable/your_image"

到你的微调器。所以它看起来像:

<Spinner
android:drawSelectorOnTop="true"
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/your_image"
android:prompt="@string/select">
</Spinner>

答案 4 :(得分:0)

这是getDropDownView

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.simple_spinner_dropdown_item, null, true);

    CheckedTextView text = (CheckedTextView) convertView.findViewById(R.id.text1);

    text.setText("TEXT");

    return convertView;
}

在创建适配器后添加它

myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);