自定义微调器

时间:2012-09-05 06:22:28

标签: android

我正在制作一个项目,我需要制作一个自定义的微调器,其中每个选项都包含一个图像视图和一个文本框。我是Android新手。任何的想法?示例代码将有很大帮助。谢谢。

1 个答案:

答案 0 :(得分:3)

是的,您可以使用Custom Adapter

<强>的test.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">

    <ImageView
     android:id="@+id/icon"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/icon"
     android:layout_marginLeft="15dip" />

    <TextView 
     android:id="@+id/weekofday"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="5dip" 
     android:layout_marginTop="10dip"
     android:text="Sunday"
     android:textColor="#000000"/>

</LinearLayout>

<强> CustomSpinner.java

public class CustomSpinner extends Activity {

    String[] DayOfWeek = {"Sunday", "Monday", "Tuesday", 
            "Wednesday", "Thursday", "Friday", "Saturday"};

    Integer[] image = {
                R.drawable.image1,
                R.drawable.image2,
                R.drawable.image3,
                R.drawable.image4,
                R.drawable.image5,
                R.drawable.image6,
                R.drawable.image7 };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);

        Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
        mySpinner.setAdapter(new MyCustomAdapter(CustomSpinner.this, R.layout.test, DayOfWeek,image)); 
    }

    public class MyCustomAdapter extends ArrayAdapter<String>{

        public MyCustomAdapter(Context context, int textViewResourceId,
                String[] objects, Integer[] image) {
            super(context, textViewResourceId, objects);
            // TODO Auto-generated constructor stub
        }

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            //return super.getView(position, convertView, parent);

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

            ImageView icon=(ImageView)row.findViewById(R.id.icon);
            icon.setImageResource(image[position]);
            return row;
        }    
    }
}

将一些图像放在可绘制的文件夹中。请查看Here this