Can't set header preference icon via ?attr

落爺英雄遲暮 提交于 2019-12-06 05:51:36

问题


I have created preference-headers.xml. I want to set header icon via ?attr, but it doesn't show the icon.

<?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>

回答1:


I solved the problem with a mixture of headers.xml and Changing the icon.

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;
    }
    }
}



回答2:


I have same problem with header icons for multiple themes.

The only way is to generate headers in code...

@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();
}


来源:https://stackoverflow.com/questions/12193355/cant-set-header-preference-icon-via-attr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!