我的问题是我想在我的应用程序中添加一个PreferenceScreen,但是我不能使用它,我也不知道为什么。
我实现了库androidx.preference:preference:1.1.0-rc01。然后我想将PreferenceScreen添加到我的XML布局中,但它并没有给出任何建议。
接下来,我将XML-Code从Android开发人员复制到XML布局中并对其进行编译,但是通过启动活动它就会中断错误:java.lang.ClassCastException: class androidx.preference.PreferenceScreen cannot be cast to android.view.View。
有人能帮我正确使用androidx.preference.PreferenceScreen吗?
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<androidx.preference.SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="Turn this option on or off"
android:title="Settings option" />
</androidx.preference.PreferenceScreen>发布于 2019-08-13 07:30:21
现在整个答案是:
implementation 'androidx.preference:preference:1.1.1'或implementation 'androidx.preference:preference-ktx:1.1.1'中。和同步格莱德尔。在res文件夹中创建名为xml的目录。main_preferences。根元素必须是androidx.preference.PreferenceScreen。<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<androidx.preference.SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="Turn this option on or off"
android:title="Settings option" />
</androidx.preference.PreferenceScreen>MainSettingsFragment的java文件。超类(意为<Classname> extends <Superclass>)必须是PreferenceFragmentCompat并覆盖onCreatePreferences。您可以复制此代码:import android.os.Bundle;
import androidx.preference.PreferenceFragmentCompat;
import com.???.???.R;
public class <YourClassname> extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// Load the preferences from an XML resource
setPreferencesFromResource(R.xml.<yourXmlFilename>, rootKey);
}
}最美妙和最好的方法是,它在创建时根据代码实现它。在SettingsActivty中添加一个FrameLayout并给它一个ID,例如fl_main_settings
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/<!-- yourID -->">
</FrameLayout>在Activity中,在onCreate方法的顶部添加以下内容:
package com.???.???;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.Html;
import android.view.MenuItem;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.text.HtmlCompat;
import com.???.???.MainSettingsFragment;
public class SettingsActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.<SettingsActivityXML(with the FrameLayout)>);
//If you want to insert data in your settings
<YourSettingsFragmentClass> settingsFragment = new <YourSettingsFragmentClass>();
settingsFragment. ...
getSupportFragmentManager().beginTransaction().replace(R.id.<YourFrameLayout>,settingsFragment).commit();
//Else
getSupportFragmentManager().beginTransaction().replace(R.id.<YourFrameLayout>,new <YourSettingsFragmentClass>()).commit();
}或--在SettingsActivityXml中实现Fragment。,但我不推荐这样做,因为启动该活动需要几秒钟
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<fragment
android:tag="frag"
android:name="com.quickme.musicme.Fragments.MainSettingsFragment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>就是这样玩得开心。
发布于 2019-08-12 21:24:30
java.lang.ClassCastException:无法将类
androidx.preference.PreferenceScreen转换为android.view.View我的布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen>
...
</androidx.preference.PreferenceScreen>注意了。
这是而不是布局。
有人能帮我正确使用androidx.preference.PreferenceScreen吗?
你可以找到所有的这里的信息。
可以定义首选项层次结构。
警告:根标记必须是a
<PreferenceScreen>,而XML资源必须放在中。
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
....
</PreferenceScreen>若要从XML属性中膨胀层次结构,请创建PreferenceFragmentCompat、重写onCreatePreferences()并提供XML资源:
class MySettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
}然后将这个Fragment添加到您的Activity中,就像添加任何其他“框架”一样。
发布于 2019-08-12 19:06:07
尝试: xml/preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="Turn this option on or off"
android:title="Settings option" />
</PreferenceScreen>我也更喜欢使用由我的活动托管的偏好片段。
class MySettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings_container, SettingsFragment())
.commit()
}
}class SettingsFragment : PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
}
}https://stackoverflow.com/questions/57466675
复制相似问题