2016-11-21 225 views
0

我想使用首选项列表添加自定义布局。但它给了我这个错误。Android PreferenceFragmentCompat自定义布局list_container错误

内容具有考虑到与id属性 'android.R.id.list_container' 那 不是一个ViewGroup类

布局:

<FrameLayout 
    android:id="@+id/list_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

样式:

<style name="BestMainTheme" parent="Theme.AppCompat.NoActionBar"> 
    <item name="colorControlNormal">@color/white</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorAccent">@color/colorAccent</item> 
    <item name="android:windowBackground">@color/windowBackground</item> 
    <item name="preferenceTheme">@style/BestAppSettingsTheme</item> 
    <item name="android:textColorPrimary">@android:color/white</item> 
    <item name="android:textColorSecondary">@android:color/darker_gray</item> 
    <item name="android:textColorHint">@android:color/darker_gray</item> 
</style> 

<style name="BestAppSettingsTheme" parent="@style/PreferenceThemeOverlay.v14.Material"> 
    <item name="android:layout">@layout/fragment_preferences</item> 
</style> 

代码:

@Override 
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 

    setPreferencesFromResource(R.xml.preferences, rootKey); 

} 

回答

1

如果您使用Support Preference版本24或更高版本,Google已将所需的ID从R.id.list_container更改为android.R.id.list_container

所以只要改变你的布局文件:

<FrameLayout 
    android:id="@android:id/list_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/>