如何改变android spinner的外观

时间:2012-10-21 10:41:37

标签: android spinner android-spinner

我在android项目中使用了一个微调器。一切都好。唯一的事情是旋转器没有显示我想要的方式。

这是图像:

Bad looking spinner

但我希望它看起来像这样:

Good looking spinner

1 个答案:

答案 0 :(得分:0)

您可以参考下面的代码。此代码将生成带有下拉箭头的微调器外观。它由

实现
  

android.R.layout.simple_spinner_item

由ArrayAdapter指定的

。 要更改其样式,您可以使用不同的布局,您可以找到here

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {

    Spinner spin;
    String spin_val;
    String[] gender = { "Male", "Female" };//array of strings used to populate the spinner
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);//setting layout
        spin = (Spinner) findViewById(R.id.spinner_id);//fetching view's id
        //Register a callback to be invoked when an item in this AdapterView has been selected
        spin.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long id) { 
                // TODO Auto-generated method stub
                spin_val = gender[position];//saving the value selected


            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }

        });
      //setting array adaptors to spinners 
      //ArrayAdapter is a BaseAdapter that is backed by an array of arbitrary objects
        ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, gender);

        // setting adapteers to spinners
        spin.setAdapter(spin_adapter);
    }


}

screen.xml

<Spinner
        android:id="@+id/spinner_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

您可以在此处获得此演示项目,以供参考here

希望这有帮助。