4

我创建了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>
4

2 回答 2

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;
    }
    }
}
于 2013-02-27T11:08:00.107 回答
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();
}
于 2013-02-07T16:28:13.527 回答