如何更改微调文本和按钮颜色

时间:2014-10-15 07:13:15

标签: android drop-down-menu spinner android-spinner

我正在尝试以编程方式更改文本的颜色和Spinner功能的按钮。如何更改我的微调器的文本和按钮颜色而不更改xml ??

以下代码:

Spinner label_Col_5_dataSpinner = new Spinner(this);
label_Col_5_dataSpinner.setId(200+Count);
String[] label_Col_5_data= new String[]{"Vivian","Xiaomi","Chris","Bill","Luyi"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,label_Col_5_data );
label_Col_5_dataSpinner.setAdapter(adapter);
label_Col_5_dataSpinner.setPadding(30, 20, 30, 20);
label_Col_5_dataSpinner.setBackgroundColor(Color.TRANSPARENT);
//label_Col_5_dataSpinner.setTextColor(Color.BLACK);

tr.addView(label_Col_5_dataSpinner);

5 个答案:

答案 0 :(得分:3)

更改按钮颜色,您可以使用此:

    Drawable spinnerDrawable = mySpinner.getBackground().getConstantState().newDrawable();


    spinnerDrawable.setColorFilter(getResources().getColor(R.color.blue_m), PorterDuff.Mode.SRC_ATOP);

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
      {  mySpinner.setBackground(spinnerDrawable); }  
    else
     {  mySpinner.setBackgroundDrawable(spinnerDrawable); }

答案 1 :(得分:0)

你可以这样使用 -

spinner.setOnItemSelectedListener(OnCatSpinnerCL);

并将其定义为 -

private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos,
                long id) {
            ((TextView) parent.getChildAt(0)).setTextSize(getResources()
                    .getDimension(R.dimen.detail_spinner_text_size));

            // Change the color here as well 
        }

        public void onNothingSelected(AdapterView<?> parent) {

        }
    };

希望这会有所帮助:)

答案 2 :(得分:0)

如果您使用的是xml ....因为它简单而强大


<强> Styles.xml

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.

    -->
    <style name="AppBaseTheme" parent="android:Theme.Holo">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- Remove shadow below actionbar -->
        <item name="android:windowContentOverlay">@null</item>
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->

        <!-- set background for entire application -->
        <item name="android:windowBackground">@color/app_background</item>

        <!-- For the resting Spinner style -->
        <item name="android:spinnerItemStyle">
            @style/spinnerItemStyle
        </item> 

        <!-- For each individual Spinner list item once clicked on -->
        <item name="android:spinnerDropDownItemStyle">
            @style/spinnerDropDownItemStyle
        </item>


    </style>



    <!-- Spinner Custom color -->

     <style name="spinnerItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
        <!-- <item name="android:padding">10dp</item> -->
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">#00aeef</item>
    </style>

    <style name="spinnerDropDownItemStyle" parent="@android:style/Widget.Holo.DropDownItem.Spinner">
       <!--  <item name="android:padding">20dp</item> -->
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">#00aeef</item>
    </style>

</resources>

答案 3 :(得分:0)

最好使用:

  Button >> on click open dialog

OR

  AutoCompleteTextView >> as u start typing it will show as dropdown list 

答案 4 :(得分:0)

对于文字颜色,希望这可行:

Spinner spinner = (Spinner)findViewById(R.id.my_spinner);
TextView tv = (TextView) spinner.getSelectedView();
tv.setTextColor(Color.BLACK);

更改按钮颜色的最佳和最简单的解决方案:

spinner.getBackground().setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);

如果您不想更改所有Spinners的其他解决方案:

Drawable spinnerDrawable = spinner.getBackground().getConstantState().newDrawable();

spinnerDrawable.setColorFilter(getResources().getColor(R.color.red), PorterDuff.Mode.SRC_ATOP);

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    spinner.setBackground(spinnerDrawable);
}else{
    spinner.setBackgroundDrawable(spinnerDrawable);
}

有关详细信息,请查看:How to change the Text color of Spinner(文字颜色)

&安培;

Change colour of small triangle on spinner in android(按钮颜色)

希望它有所帮助!祝你好运!