【问题标题】:android Number picker text colourandroid数字选择器文本颜色
【发布时间】:2016-02-12 06:33:34
【问题描述】:

我希望数字选择器中的值是白色的。 我做了以下代码,使其默认为黑色。

XML 代码:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@layout/gradient_blue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:textSize="25sp"
        android:textColor="#fff"
        android:textStyle="bold"
        android:text="Please select difficulty level"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@drawable/category_seperator"/>

    <LinearLayout
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">

        <NumberPicker
            android:id="@+id/numberPicker1"
            android:layout_width="wrap_content"
            android:minWidth="100dp"
            android:layout_margin="10dp"

            android:layout_height="wrap_content"/>
            <!--#019687-->
        <Button
            android:background="#019687"

            android:id="@+id/setting_confirm"
            android:layout_width="wrap_content"
            android:layout_margin="10dp"
            android:layout_height="wrap_content"
            android:textSize="30sp"
            android:textColor="#fff"
            android:padding="15dp"
            android:text="OK"/>


    </LinearLayout>

</LinearLayout>

Java代码:

    final Dialog d = new Dialog(context);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);

d.setContentView(R.layout.dialog_settings);

final NumberPicker n = (NumberPicker) d.findViewById(R.id.numberPicker1);

String easy = Html.fromHtml("<font color=\"white\">EASY</font>").toString();
String medium = Html.fromHtml("<font color=\"white\">MEDIUM</font>")+"";
String hard = Html.fromHtml("<font color=\"white\">HARD</font>")+"";
String tough = Html.fromHtml("<font color=\"white\">TOUGH</font>")+"";

//        String[] a = {"EASY", "MEDIUM", "HARD", "TOUGH"};
String [ ] a = {easy,medium,hard,tough};
n.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
n.setDisplayedValues(a);


n.setMaxValue(3);
n.setMinValue(0);
n.setWrapSelectorWheel(true);

d.show();

我尝试使用 html 更改颜色,如代码所示。 我还尝试使用 getChildAt 更改颜色并将其转换为 TextView 导致应用程序崩溃。

【问题讨论】:

  • 你能发布你的崩溃日志吗,因为我使用了完全相同的方法并且它有效。
  • 它并没有崩溃,实际上与其他一些答案混淆了,但对我来说都没有工作在返回语句之前完成 try 块显示日志但颜色似乎没有改变使用该方法作为 setNumberPickerTextColor (n,R.color.white);
  • 这是因为你没有在这个方法中传递资源颜色。你传递了Color 类的颜色。例如,Color.RED
  • 我应该把它作为答案发布吗?

标签: android textcolor numberpicker


【解决方案1】:

只需在您的 style.xml 文件中使用 textColorPrimary 属性:

<style name="Theme.MyTheme" parent="@android:style/Theme.Holo.NoActionBar.Fullscreen" >
    <item name="android:textColorPrimary">#FFFFFF</item>

【讨论】:

    【解决方案2】:

    复制: Change the text color of NumberPicker

    颜色参数应该来自Color类例如Color.WHITE

    import java.lang.reflect.Field;
    
    public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color)
    {
        final int count = numberPicker.getChildCount();
        for(int i = 0; i < count; i++){
            View child = numberPicker.getChildAt(i);
            if(child instanceof EditText){
                try{
                    Field selectorWheelPaintField = numberPicker.getClass()
                        .getDeclaredField("mSelectorWheelPaint");
                    selectorWheelPaintField.setAccessible(true);
                    ((Paint)selectorWheelPaintField.get(numberPicker)).setColor(color);
                    ((EditText)child).setTextColor(color);
                    numberPicker.invalidate();
                    return true;
                }
                catch(NoSuchFieldException e){
                    Log.w("setNumberPickerTextColor", e);
                }
                catch(IllegalAccessException e){
                    Log.w("setNumberPickerTextColor", e);
                }
                catch(IllegalArgumentException e){
                    Log.w("setNumberPickerTextColor", e);
                }
            }
        }
        return false;
    }
    

    【讨论】:

      【解决方案3】:

      根据我们在 cmets 中的讨论,代码 here 需要来自 Color 类的参数。使用此类传递颜色。

      【讨论】:

        【解决方案4】:

        我正在使用 Kotlin:

        只需以编程方式设置数字选择器的颜色:

        numberPicker.textColor = 0xAARRGGBB.toInt()
        

        示例:

        numberPicker.textColor = 0xAA3068D8.toInt()
        

        【讨论】:

        • 仅限 android q (API 29) 及更高版本
        猜你喜欢
        • 2012-12-14
        • 1970-01-01
        • 1970-01-01
        • 2012-11-12
        • 1970-01-01
        • 1970-01-01
        • 2018-06-16
        • 2013-12-16
        • 2012-10-26
        相关资源
        最近更新 更多