请帮帮我,这是我最近两天打砖墙,无法弄明白.
我有一个简单的PreferenceFragment,并希望从一个Activity中膨胀它.
这是res/xml/preferences.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="preferences">
<PreferenceCategory
android:title="@string/notification_basic"
android:key="notification">
<CheckBoxPreference
android:key="enable_notification"
android:summary="@string/enable_notification"
android:title="@string/notification_title"
android:defaultValue="false">
</CheckBoxPreference>
<CheckBoxPreference
android:key="enable"
android:summary="@string/enable_instant_notification"
android:title="@string/notify_me_instantly"
android:dependency="enable_notification"
android:defaultValue="false">
</CheckBoxPreference>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/sign_up_notify_me_where_section"
android:key="locations">
<MultiSelectListPreference
android:dialogTitle="@string/sign_up_notify_me_where_title"
android:key="location"
android:summary="@string/sign_up_notify_me_where"
android:title="@string/sign_up_notify_me_where_title"
android:entries="@array/locations"
android:entryValues="@array/locations_values"
android:defaultValue="@array/empty_array"
android:dependency="enable_notification"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/sign_up_notify_me_type_section"
android:key="types">
<MultiSelectListPreference
android:dialogTitle="@string/sign_up_notify_me_type_title"
android:key="type"
android:summary="@string/sign_up_notify_me_type"
android:title="@string/sign_up_notify_me_type_title"
android:entries="@array/types"
android:entryValues="@array/types_values"
android:defaultValue="@array/empty_array"
android:dependency="enable_notification"
/>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/sign_up_notify_me_who_section"
android:key="units">
<MultiSelectListPreference
android:dialogTitle="@string/sign_up_notify_me_who_title"
android:key="units"
android:summary="@string/sign_up_notify_me_who"
android:title="@string/sign_up_notify_me_who_title"
android:entries="@array/units"
android:entryValues="@array/units_values"
android:defaultValue="@array/empty_array"
android:dependency="enable_notification"
/>
</PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
活动:
public class HomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在"设置"页面中使用SwitchPreference,但我无法让侦听器工作.我正在使用自定义小部件布局来设置切换样式,因为我找不到如何使用主题来实现这一点.在我的settings.xml中,我有以下内容:
<SwitchPreference
android:key="uitestmode"
android:summary="Enable to display test data in fragments"
android:title="UI Test Mode"
android:widgetLayout="@layout/widget_custom_switch_on_off" >
</SwitchPreference>
Run Code Online (Sandbox Code Playgroud)
我的SettingsFragment看起来像:
SwitchPreference uiTestModePref = (SwitchPreference) findPreference("uitestmode");
uiTestModePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.d(preference.getKey(), newValue.toString());
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
和我的交换机的自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_switch_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:textColor="@android:color/white"
android:textIsSelectable="false"
android:textSize="18sp"
android:textOn="On"
android:textOff="Off"
android:textStyle="bold"
android:thumb="@drawable/carcast_switch_inner_holo_dark"
android:track="@drawable/carcast_switch_track_holo_dark" />
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我单击开关时,不会调用侦听器.有谁知道为什么以及如何解决它?
在使用preference-v7支持库(版本23.1.0)中的默认PreferenceThemeOverlay时,我遇到了以下问题.从API 22开始,我的PreferenceFragmentCompat在我的首选项列表的左侧和右侧添加了一个丑陋的附加填充.
的build.gradle:
compile 'com.android.support:appcompat-v7:23.1.0'
Run Code Online (Sandbox Code Playgroud)
styles.xml:
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
Run Code Online (Sandbox Code Playgroud)
我没有在stackoverflow上找到任何有用的解决方案后,我自己写了一个解决方法.我只是想与你们分享.
android android-support-library preferencefragment preference-v7
我不知道这个错误。我从Google开发人员控制台获得了这些消息。z3,htc m8,m9等某些设备无法执行我的应用。有人可以给我一些有关该错误的提示吗?
堆栈跟踪 :
java.lang.RuntimeException: Unable to start activity ComponentInfo{package name/package name.SettingsActivity}: android.view.InflateException: Binary XML file line #31: Error inflating class com.android.internal.widget.ActionBarContainer
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2499)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2563)
at android.app.ActivityThread.access$800(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1438)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:209)
at android.app.ActivityThread.main(ActivityThread.java:5900)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1005)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:800)
Caused by: android.view.InflateException: Binary XML file line #31: Error inflating class com.android.internal.widget.ActionBarContainer
at android.view.LayoutInflater.createView(LayoutInflater.java:633)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3659)
at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3755)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:403)
at android.app.Activity.setContentView(Activity.java:2174)
at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:553)
at …
Run Code Online (Sandbox Code Playgroud) 我想做一个普通的PreferenceActivity(旧样式,没有标题),但是使用片段而不使用addPreferencesFromResource(id)
(因为弃用).
现在我把它放在我的onCreate
:
getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesFragment()).commit();
Run Code Online (Sandbox Code Playgroud)
我PreferencesFragment
看起来像这样:
public static class PreferencesFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
Run Code Online (Sandbox Code Playgroud)
显然我也实现了这个isValidFragment
方法:
@Override
protected boolean isValidFragment(String fragmentName) {
return PreferencesFragment.class.getName().equals(fragmentName)
|| InnerFragment.class.getName().equals(fragmentName);
}
Run Code Online (Sandbox Code Playgroud)
它工作得很好.然而,当PreferenceScreen
我在我的内心时有一个问题preferences.xml
:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- ... -->
<PreferenceScreen android:fragment="net.chaozspike.batterynotifier.SettingsActivity$InnerFragment"
android:summary="@string/settings_activity_prefs_display_night_mode_sum"
android:title="@string/settings_activity_prefs_display_night_mode" />
<!-- ... -->
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
我有以下问题:新的首选项屏幕显示在正常的屏幕之上,我知道这可能是因为我更换了默认设置R.id.content
.截图:
但是我想在不使用已弃用的东西或标题的情况下解决此问题.这有可能实现吗?
android preferenceactivity preferencescreen preferencefragment
我按照谷歌指南(https://developer.android.com/guide/topics/ui/settings)将 PreferenceFragmentCompat 添加到我的应用程序中。
此外,我还有另一个片段(Fragment A),其中包含 MotionLayout 中的 RecyclerView 。MotionLayout用于一个小动画,它通过onSwipe将RecyclerView扩展到垂直全屏。
现在我遇到以下问题:在调用 PreferenceFragment 然后返回到 Fragment A 后,MotionLayout onSwipe 动画的性能确实很差/滞后。根据日志,滑动时会多次调用 onDraw 方法。
有趣的事实是,当我还没有启动 PreferenceFragment 时,onDraw 方法只被调用一次(@onCreate)?!
此外,调用我的其他片段的任意片段 X 不会影响我的片段 A的性能?!
这可能是什么原因?我该如何继续我的调查?
android android-preferences android-fragments preferencefragment android-motionlayout
我用的是安卓x。在设置片段中,我想创建“首选项”按钮并单击它们以触发一些单独的事件。
如何在特定首选项上实现点击侦听器?
那是我的一些代码:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
public class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.preference);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
view.setBackgroundColor(getResources().getColor(R.color.graylight));
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setTitle(R.string.action_settings);
toolbar.setLogo(R.drawable.ic_settings_white_24dp);
PreferenceManager preferenceManager = getPreferenceManager();
PreferenceScreen preferenceScreen = getPreferenceScreen();
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
和 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 …
Run Code Online (Sandbox Code Playgroud) 如何创建设置屏幕?我应该使用PreferenceActivity还是 PreferenceFragment?
注意 -文档没有帮助
我已经尝试过的:
我想要的是使用PreferenceFragment的简单实现