0

我正在动态创建一个 Spinner,如下面的代码:

private void createCoordinationSpinner() {
    TextView tvwCoordination = new TextView(this);
    this.spinnerCoordination = new Spinner(this);

    //Some code to setup textview... 

    LinearLayout.LayoutParams paramsSpinnerCoordination = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    paramsSpinnerCoordination.setMargins(10, 0, 10, 0);

    this.spinnerCoordination.setLayoutParams(paramsSpinnerCoordination);
    this.spinnerCoordination.setBackgroundResource(R.drawable.rounded_border);
    this.spinnerCoordination.setPrompt("Coordination");
    this.spinnerCoordination.setOnItemSelectedListener(spinnerItemClickListener);

    //Adding both views to an existing LinearLayout, that is possible to have another views, instead of spinner
    linearSchoolCoordination.addView(tvwCoordination, 0);
    linearSchoolCoordination.addView(spinnerCoordination, 1);
    linearSchoolCoordination.setGravity(Gravity.BOTTOM);
}

//Thats the line that sets the adapter:
adapterCoordination = new ItemSpinnerAdapter(context, R.layout.coordinationspinnerlayout, arrayCoordination);

rounded_border.xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="@color/gray_dialog" /> 
    <padding android:left="8dp" 
        android:top="8dp" 
        android:right="8dp" 
        android:bottom="8dp" /> 
    <corners android:radius="20dp" /> 
</shape>

协调微调器布局.xml:

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

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/transparent2"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent2"
            android:gravity="center_vertical" >

            <ImageView  android:layout_height="wrap_content" 
                        android:layout_width="wrap_content" 
                        android:id="@+id/imageSpinnerItem" 
                        android:src="@drawable/itemlistviewicon_nochildren" 
                        android:layout_marginTop="5dp" 
                        android:layout_marginLeft="8dp" 
                        android:layout_marginBottom="5dp"/> 

            <TextView   android:layout_height="wrap_content" 
                        android:layout_width="wrap_content" 
                        android:id="@+id/tvwSpinnerCoordination" 
                        android:layout_marginLeft="10dp" 
                        android:textSize="16dip" 
                        android:textColor="@color/black" 
                        android:text="TextView" 
                        android:layout_weight="1"/> 

            <ImageView  android:layout_height="20dp" 
                        android:layout_width="20dp" 
                        android:id="@+id/imageSpinnerDownArrow" 
                        android:src="@drawable/spinnerdownarrow" 
                        android:layout_marginTop="5dp" 
                        android:layout_marginBottom="5dp" 
                        android:layout_marginRight="10dp"/> 
        </LinearLayout> 

        <View   android:background="@color/silver" 
                android:layout_height="1dp" 
                android:layout_width="fill_parent" 
                android:id="@+id/linearSpinnerSeparator"/> 

    </LinearLayout> 

</LinearLayout>

最后是适配器 ItemSpinnerAdapter.java:

public class ItemSpinnerAdapter extends ArrayAdapter<CoordinationData> 
{
    private int tvwID;
    private List<CoordinationData> coordinationList;

    public ItemSpinnerAdapter(Context context, int textViewResourceId, List<CoordinationData> coordinationList) 
    {
        super(context, textViewResourceId, coordinationList);

        this.tvwID = textViewResourceId;
        this.coordinationList = coordinationList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewHolder holder;
        View view = convertView;

        if (view == null) 
        {
           LayoutInflater layInf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           view = layInf.inflate(tvwID, null);

           holder = new ViewHolder();
           holder.tvwDescription = (TextView) view.findViewById(R.id.tvwSpinnerCoordination);
           holder.description = coordinationList.get(position).getCoordinationName();

           view.findViewById(R.id.linearSpinnerSeparator).setVisibility(View.GONE);
           view.setTag(holder);

        } 
        else 
        {
            holder = (ViewHolder) view.getTag();
            holder.description = coordinationList.get(position).getCoordinationName();
        }

        if (holder.tvwDescription != null) 
            holder.tvwDescription.setText(holder.description);

        return view;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) 
    {
        ViewHolder holder;
        View view = convertView;

        if (view == null) 
        {
            LayoutInflater layInf = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layInf.inflate(tvwID, null);

            holder = new ViewHolder();
            holder.tvwDescription = (TextView) view.findViewById(R.id.tvwSpinnerCoordination);
            holder.tvwDescription.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
            holder.description = coordinationList.get(position).getCoordinationName();

            view.findViewById(R.id.imageSpinnerDownArrow).setVisibility(View.GONE);
            view.setTag(holder);

        } 
        else 
        {
            holder = (ViewHolder) view.getTag();
            holder.description = coordinationList.get(position).getCoordinationName();
        }

        if (holder.tvwDescription != null) 
            holder.tvwDescription.setText(holder.description);

    return view;
}

public class ViewHolder 
{
    TextView tvwDescription;
    String description;
}
}

将它添加到我活动的 XML 布局文件中已经存在的 LinearLayout 中。与我的 CustomSpinnerAdapter 一起,它运行良好。但我仍然需要更改一些属性并且我被卡住了......由于我的应用程序使用蓝色文本和背景,我想设置微调项突出显示颜色的选择。它的默认值为橙色。

由于我正在为 API 10 进行开发,因此我使用了有限的 Spinner 方法。

有什么建议么?提前致谢!

编辑:是否可以更改提示标题背景颜色?

EDIT2:添加更多代码。

4

3 回答 3

0

编辑

您正在使用微调器,因此您需要实现[getDropDownView][1].


如果要更改视图的选定颜色,则需要像这样创建一个 Color List Drawable

在颜色/list_color.xml

<selector>
  <item android:state_selected="true">SomeColor</item>
</selector>

在你看来

<YourView
  android:background="@color/list_color" />

有关更多信息,请参阅此资源

于 2012-09-13T18:48:30.010 回答
0

经过长时间寻找解决方案,

我发现由于我正在开发的 API 版本,我无法动态设置样式或更改其选择颜色。

于 2012-09-25T20:35:14.177 回答
0
take the custum adapter 



mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View arg1,
                    int position, long arg3) {

                _position = position;
                LinearLayout linear_ayout = (LinearLayout)parent.getChildAt(0);
                linear_ayout.setBackgroundColor(Color.TRANSPARENT);
                TextView tv = (TextView) linear_ayout.getChildAt(0);
                tv.setTextColor(Color.WHITE);

                gname_spinner=mySpinner.getSelectedItem().toString();
                if (mySpinner.getSelectedItem().toString().equalsIgnoreCase("All Groups")) 
                    filItemAdapter("A", ""); 
                    else
                    filItemAdapter("G", mySpinner.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                Logger.d("******", "not selected");
                 filItemAdapter("A", "");
            }

        });



    MyCustomAdapter custom_adapter = new MyCustomAdapter(
                getApplicationContext(), R.layout.custum_spinner_group,
                group_list);

        mySpinner.setAdapter(custom_adapter);





public class MyCustomAdapter extends ArrayAdapter<String> {

        List<String> group_list;

        public MyCustomAdapter(Context context, int textViewResourceId,
                List<String> group_list) {
            super(context, textViewResourceId, group_list);
            this.group_list = group_list;

        }

        @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.custum_spinner_group, parent, false);
            TextView text_group = (TextView) row.findViewById(R.id.text_group_name);
            LinearLayout layout = (LinearLayout) row
                    .findViewById(R.id.layout_group_name);
            text_group.setText(group_list.get(position));           

            if (position == _position) {

                layout.setBackgroundColor(getResources().getColor(R.color.spinner_background));
                text_group.setTextColor(getResources().getColor(R.color.white));

                } else {

                layout.setBackgroundColor(Color.TRANSPARENT);
                text_group.setTextColor(getResources().getColor(R.color.spinner_background));
                }

            Log.d("selected group", ":"+text_group.getText());


            return row;

        }
    }
于 2014-09-25T11:27:39.723 回答