Android 偏好设置摘要默认颜色?

发布于 2024-09-30 00:41:58 字数 177 浏览 9 评论 0原文

我已经在真正的手机中安装了我的应用程序,即使在模拟器中的所有文本 偏好摘要似乎是相同的颜色,在真正的手机中,颜色是不同的(某种蓝色......但我想这取决于手机的型号)。

如何将此颜色设置为我的自定义首选项组件? (我已经实现了自己的搜索栏,其摘要文本颜色与所有其他组件文本颜色不同......)。

谢谢!

I have installed my app in a real phone, and even though in the emulator all the texts of the
preferences summaries seem to be in the same color, in the real phone the color is different (some kind of blue... but I guess it depends on the phone's model).

How can I set this color to my custom preference component?
(I have implemented my own seek bar, and its summary text color is different from all the other components text color...).

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

丑丑阿 2024-10-07 00:41:58
Preference pUpdate = findPreference("sys_setting_update");
pUpdate.setSummary(Html.fromHtml("<font color=\"#B0C4DE\">This is content</font>"));

使用 Html.fromHtml("This is content") 来设置Summary

Preference pUpdate = findPreference("sys_setting_update");
pUpdate.setSummary(Html.fromHtml("<font color=\"#B0C4DE\">This is content</font>"));

use Html.fromHtml("<font color=\"#B0C4DE\">This is content</font>") to setSummary

许一世地老天荒 2024-10-07 00:41:58

我找到了这些: android:textAppearance="?android:attr/textAppearanceLarge"

和 android:textAppearance="?android:attr/textAppearanceSmall"
似乎可以解决问题。

I found these: android:textAppearance="?android:attr/textAppearanceLarge"

and android:textAppearance="?android:attr/textAppearanceSmall"
seem to do the trick.

脸赞 2024-10-07 00:41:58

我已经找到了一种方法来检索运行应用程序的 Android 设备使用的默认颜色。这有点棘手,并且需要您从 Activity 的另一个首选项摘要视图中检索显示的颜色并将其存储在运行时。

然后,您可以在其他首选项的其他视图中使用相同的颜色代码,确保您始终获得分配给标准首选项的相同颜色代码。我是这样做的:

我的首选项活动有一个正常的 CheckBoxPreference,我用它来激活或停用服务。我扩展了 CheckBoxPreference,如下所示,因此我的扩展在 rutime 中检索 Android 最终赋予该 CheckBoxPreference 的摘要的默认颜色:

public class MyCheckBoxPreference extends android.preference.CheckBoxPreference {

    private static int sSummaryColor = Color.WHITE;
    private static boolean sInitialized = false;

    public MyCheckBoxPreference(Context context) {
        super(context);
    }

    public MyCheckBoxPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override    
    public void onBindView(View view) {
        super.onBindView(view);
        if (!sInitialized) {
            sSummaryColor = getSummaryColor(view);
            sInitialized = true;
        }
    }

    private int getSummaryColor(View view) {

       int color = Color.WHITE;

        // Gets the color android gave to the summary by default
        TextView summaryView = (TextView) view.findViewById(android.R.id.summary); 
        if (summaryView != null) {
           ColorStateList list = summaryView.getTextColors();
            if (list != null) {
                color = list.getDefaultColor();
            }
        }
        return color;
    }

    public static int getSummaryColor() {
        return sSummaryColor;
    }
}

在我的preferences.xml 中,我将该首选项实例化为 MyCheckBoxPreference,而不仅仅是 CheckBoxPreference:

<org.yourpackage.MyCheckBoxPreference
                android:title="@string/preference_title_activate" 
                android:defaultValue="false" 
                android:summary="@string/preference_summary_activate_off" 
                android:summaryOff="@string/preference_summary_activate_off"
                android:key="preference_activate">
</org.yourpackage.MyCheckBoxPreference>

MyCheckBoxPreference 必须在之前实例化一次使用 MyCheckBoxPreference.getSummaryColor() 检索摘要颜色。

现在您可以从 onBindView(View) 设置其他自定义首选项的颜色:

public class MyCustmizedPreference extends Preference {
    public MyCustmizedPreference (Context context) {
        super(context);
        setLayoutResource(R.layout.my_customized_preference);
    }

    @Override
    public void onBindView(View view) {
        super.onBindView(view);

        TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
        if (summaryView != null) {
            summaryView.setTextColor(MyCheckBoxPreference.getSummaryColor());
        }        
    }
}

它实际上可以在 Samsung Galaxy S 下运行。我还测试过它在模拟器下不会破坏任何内容。

I have figured out a way to retrieve the default color used by the Android device your application is running in. It is a bit tricky and requieres that you retrieve the color being shown from another Preference Summary View of your activity and store it in runtime.

Then you can use the same color code in other Views of other preferences, assuring that you will allways get the same color code Android assigned to the standard preferences. Here is how I did it:

My preferences activity has a normal CheckBoxPreference that I use to activate or deactivate a service. I have extended CheckBoxPreference as follows, so my extension retrieves in rutime the default color Android finally gave to the summary of that CheckBoxPreference:

public class MyCheckBoxPreference extends android.preference.CheckBoxPreference {

    private static int sSummaryColor = Color.WHITE;
    private static boolean sInitialized = false;

    public MyCheckBoxPreference(Context context) {
        super(context);
    }

    public MyCheckBoxPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyCheckBoxPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override    
    public void onBindView(View view) {
        super.onBindView(view);
        if (!sInitialized) {
            sSummaryColor = getSummaryColor(view);
            sInitialized = true;
        }
    }

    private int getSummaryColor(View view) {

       int color = Color.WHITE;

        // Gets the color android gave to the summary by default
        TextView summaryView = (TextView) view.findViewById(android.R.id.summary); 
        if (summaryView != null) {
           ColorStateList list = summaryView.getTextColors();
            if (list != null) {
                color = list.getDefaultColor();
            }
        }
        return color;
    }

    public static int getSummaryColor() {
        return sSummaryColor;
    }
}

In my preferences.xml I instantiate that preference as MyCheckBoxPreference instead of just CheckBoxPreference:

<org.yourpackage.MyCheckBoxPreference
                android:title="@string/preference_title_activate" 
                android:defaultValue="false" 
                android:summary="@string/preference_summary_activate_off" 
                android:summaryOff="@string/preference_summary_activate_off"
                android:key="preference_activate">
</org.yourpackage.MyCheckBoxPreference>

The MyCheckBoxPreference has to be instantiated once before retrieving the summary color with MyCheckBoxPreference.getSummaryColor().

Now you can set the color of other customized preferences from onBindView(View):

public class MyCustmizedPreference extends Preference {
    public MyCustmizedPreference (Context context) {
        super(context);
        setLayoutResource(R.layout.my_customized_preference);
    }

    @Override
    public void onBindView(View view) {
        super.onBindView(view);

        TextView summaryView = (TextView) view.findViewById(android.R.id.summary);
        if (summaryView != null) {
            summaryView.setTextColor(MyCheckBoxPreference.getSummaryColor());
        }        
    }
}

It actually works under Samsung Galaxy S. I have also tested that it doesn't break anything under the emulator.

你的背包 2024-10-07 00:41:58

Samsung Galaxy S 手机有自己的首选项布局,并为摘要行指定了文本颜色。即使指定了 TextAppearance.Small,布局的 textColor 属性也会覆盖文本外观。

The Samsung Galaxy S phones have their own Preference layout with the text color specified for the Summary line. Even though a TextAppearance.Small is specified the textColor attribute of the layout is overriding the text appearance.

土豪我们做朋友吧 2024-10-07 00:41:58

我认为这是不可能的。我可以更改背景颜色和标题文本颜色,但不能更改摘要颜色。

背景:

getListView().setBackgroundColor(Color.TRANSPARENT);

标题文字:

Preference yourpreference = findPreference("yourpreference");
TextView tv = (TextView)yourpreference.getView(null, getListView());
tv.setTextColor(...);

抱歉,我无法提供更多帮助......

I don't think this is possible. I am able to change the background color and the title text color, but not the summary color.

Background:

getListView().setBackgroundColor(Color.TRANSPARENT);

Title text:

Preference yourpreference = findPreference("yourpreference");
TextView tv = (TextView)yourpreference.getView(null, getListView());
tv.setTextColor(...);

Sorry I couldn't help more...

薄暮涼年 2024-10-07 00:41:58

我遇到了同样的问题,并且一直在尝试自定义搜索栏首选项的样式。最后,seekBarPreference.javaonCreateView 方法中的这些行以默认文本颜色显示首选项摘要:

TextView summaryText = new TextView(getContext());
summaryText.setText(getSummary());
summaryText.setTextAppearance(getContext(), android.R.style.TextAppearance_Small);

我在 preference_screen.xml 上使用它:

<com.asdasf.SeekBarPreferencias
        android:key="@string/pref_seekBar_distance_key"
        android:id="@+id/mySeekBarPreference"
        android:title="@string/pref_seekBar_distance_title"
        android:summary="@string/pref_seekBar_distance_summary"        
        android:max="50"
        android:defaultValue="12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

我希望它会有用的...(而且我的第一个答案写得很好)
看待!

I had the same problem and I've been experimenting with my custom seekbar-preference's style. Finally these lines in onCreateView method of seekBarPreference.java show preference's summary with default text color:

TextView summaryText = new TextView(getContext());
summaryText.setText(getSummary());
summaryText.setTextAppearance(getContext(), android.R.style.TextAppearance_Small);

I use it on preference_screen.xml:

<com.asdasf.SeekBarPreferencias
        android:key="@string/pref_seekBar_distance_key"
        android:id="@+id/mySeekBarPreference"
        android:title="@string/pref_seekBar_distance_title"
        android:summary="@string/pref_seekBar_distance_summary"        
        android:max="50"
        android:defaultValue="12"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

I hope it will be useful...(and that I have written well my first answer)
Regard!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文