5

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

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

谢谢!

4

6 回答 6

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

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

于 2011-09-01T06:01:37.470 回答
3

我找到了这些:android:textAppearance="?android:attr/textAppearanceLarge" 并且android:textAppearance="?android:attr/textAppearanceSmall" 似乎可以解决问题。

于 2010-11-14T19:12:42.870 回答
3

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

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

我的首选项活动有一个正常的 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.getSummaryColor() 检索摘要颜色之前,必须实例化一次 MyCheckBoxPreference。

现在您可以从 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());
        }        
    }
}

它实际上可以在三星 Galaxy S 下运行。我还测试过它不会在模拟器下破坏任何东西。

于 2012-01-02T21:39:38.447 回答
2

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

于 2011-03-28T22:14:17.580 回答
1

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

背景:

getListView().setBackgroundColor(Color.TRANSPARENT);

标题文字:

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

对不起,我帮不了更多……

于 2010-11-08T18:44:30.673 回答
0

我有同样的问题,我一直在试验我的自定义搜索栏偏好的风格。onCreateView最后,显示首选项摘要方法中的这些行seekBarPreference.java带有默认文本颜色:

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" />

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

于 2014-12-06T18:43:54.457 回答