无法通过?attr设置标题首选项图标

时间:2012-08-30 09:00:10

标签: android android-preferences

我创建了preference-headers.xml。我想通过?attr设置标题图标,但它不会显示图标。

<?xml version="1.0" encoding="utf-8"?>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" >

<header
    android:fragment="com.armsoft.mtrade.activities.PreferencesActivity$PrefsAboutFragment"
    android:icon="?attr/menuIconAbout"
    android:title="@string/about" />

</preference-headers>

2 个答案:

答案 0 :(得分:7)

我使用headers.xml和更改图标的混合解决了这个问题。

public void onBuildHeaders(List<Header> target) {
    this.loadHeadersFromResource(R.xml.pref_headers, target);
    // search for the current header by comparing the titelRes Ids
    for (Header header : target) {
        if (header.titleRes == R.string.pref_style_title) {
            int themeDependIcon = ... //load the needed icon
            header.iconRes = themeDependIcon ;
            break;
    }
    }
}

答案 1 :(得分:1)

我对多个主题的标题图标存在同样的问题。

唯一的方法是在代码中生成标题...

@Override
public void onBuildHeaders(List<Header> target) {
    final TypedArray a = getTheme().obtainStyledAttributes(R.style.<YourTheme>, new int[] = { R.attr.icon1, R.attr.icon2});
    final Header h1 = new Header();
    h1.title = "title 1";
    h1.iconRes = a.getResourceId(0, 0);
    h1.fragment = "<your_fragment_path>";
    h1.fragmentArguments = new Bundle();
    h1.fragmentArguments.putString("id", "fragment1");
    final Header h2 = new Header();
    h2.title = "title 2";
    h2.iconRes = a.getResourceId(1, 0);
    h2.fragment = "<your_fragment_path>";
    h2.fragmentArguments = new Bundle();
    h2.fragmentArguments.putString("id", "fragment2");
    target.add(h1);
    target.add(h2);
    a.recycle();
}