2016-11-21 221 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"/>