【问题标题】:Android Spinner created dynamically change selected item background colorAndroid Spinner 创建动态更改所选项目背景颜色
【发布时间】:2014-09-25 19:51:10
【问题描述】:

我正在动态创建 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>

coordinationspinnerlayout.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:添加更多代码。

【问题讨论】:

    标签: android spinner


    【解决方案1】:

    编辑:

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


    如果你想改变你的视图选择的颜色,你需要像这样创建一个 Color List Drawable

    在颜色/list_color.xml 中

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

    在你看来

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

    更多信息请见this resource

    【讨论】:

    • 感谢您的回答,但没有奏效。我已经为它设置了背景。我尝试将现有选择器设置为背景资源。
    • 那么你需要发布所有相关的代码,因为据我所知,这绝对是你遇到的问题。
    • 好的,我刚刚添加了适配器和 XML。
    • 但是getDropDownView是在Adapter的类中实现的。能具体点吗?
    【解决方案2】:
    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;
    
            }
        }
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2016-12-03
        • 1970-01-01
        • 1970-01-01
        • 2010-12-07
        • 1970-01-01
        • 2013-06-03
        • 1970-01-01
        • 1970-01-01
        • 2011-12-26
        相关资源
        最近更新 更多