Custom PreferenceScreen in PreferenceScreen

2019-08-04 09:13发布

I have a standard preference page in my app. But from this preference page i want the user to be able to navigate to an other PreferenceScreen with a custom layout, when user presses "Manage Favorits.

This is the custom layout i have in mind:

enter image description here

Is it possible to use custom layout on "sub-preference-screens"?

Thanks!

1条回答
Summer. ? 凉城
2楼-- · 2019-08-04 09:54

Yes, you can launch a separate activity using intent.

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceScreen android:title="@string/your_title"
    android:summary="@string/your_string">

        <intent android:targetClass="your.package.YourClass"
            android:targetPackage="your.package" />
    </PreferenceScreen>

</PreferenceScreen>

As long as you want to specify your own layout you would need to extend Preference class.

Use setLayoutResource() to define your layout in your constructor. Constructor needs to be

public YourClass(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayoutResource(R.layout.your_custom_layout);
    }

You can also check this.

查看更多
登录 后发表回答