我尝试使用上方的工具栏为我的应用创建设置窗口,然后输入后退按钮等。但我总是这个错误。
错误 渲染期间引发异常:android.support.design.widget.AppBarLayout无法强制转换为android.preference.Preference
preferenze.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory>
<ListPreference android:key="colore"
android:summary="ciao"
android:title="About "
android:defaultValue="#000000"
android:dialogTitle="colore di sfondo" />
</PreferenceCategory>
</PreferenceScreen>
的settings.xml
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:toolbarId="@+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
app:contentScrim="?attr/colorPrimary">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
android:minHeight="?attr/actionBarSize"
android:id="@+id/toolbar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:id="@+id/activity_tips"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.twisterandroid.Tips_5">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:weightSum="1">
<include layout="@xml/preferenze"/>
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
答案 0 :(得分:0)
我也有这个问题并花了一些时间来解决它。 步骤进行:
- 使用要设置活动的AppCompatPreferenceActivity
进行扩展。
从here获取此类,并将其作为java类放入项目中。
- 参考片段:
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_fragment);
}
}
- 在首选项/设置片段布局(首选项屏幕)中,创建一个PreferenceCategory
并将其置于最顶层,作为布局的第一个元素,并将工具栏作为布局添加到该布局中:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:layout="@layout/settings_toolbar" />
<!--<rest of the elements below it here>-->
</PreferenceScreen>
- 创建settings_toolbar
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
- 最终在“设置活动”中填充工具栏,这里是Settings/PreferencesActivity
的代码:
public class SettingsActivity extends AppCompatPreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.settings_toolbar, (ViewGroup) findViewById(android.R.id.content));
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("Settings or Preferences, etc ... ");
setSupportActionBar(toolbar);
SettingsFragment settingsFragmnet= new SettingsFragment();
getFragmentManager().beginTransaction().replace(android.R.id.content, settingsFragmnet).commit();
}
}