Android PreferenceFragmentCompat为首选项添加边距
考虑这个例子: android中有一个最小的活动,它将此布局扩展为根目录:Android PreferenceFragmentCompat为首选项添加边距,android,androidx,preferencefragment,Android,Androidx,Preferencefragment,考虑这个例子: android中有一个最小的活动,它将此布局扩展为根目录: <!-- FILE activity_preference.xml --> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layo
<!-- FILE activity_preference.xml -->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/settings_container"/>
</LinearLayout>
然后我想用我的设置替换设置容器。
我使用的是当前的androidx Jetpack库,它进入应用程序的gradle文件:
implementation 'androidx.preference:preference:1.1.0'
我还根据自己的需要创建了一个自定义PreferenceFragmentCompat,但现在它并没有做太多:
public class MyPreferenceFragmentCompat extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// get the screen
PreferenceScreen preferenceScreen = getPreferenceManager().createPreferenceScreen(getContext());
// add item(s)
CheckBoxPreference item_Confirmation;
item_Confirmation = new CheckBoxPreference(this);
item_Confirmation.setKey("config_Confirmation");
item_Confirmation.setTitle("Confirmation");
item_Confirmation.setSummary("Confirmation");
item_Confirmation.setDefaultValue(false);
preferenceScreen.addPreference(item_Confirmation);
// set this screen as default
setPreferenceScreen(preferenceScreen);
}
下面是activity如何处理我的片段:
getSupportFragmentManager().beginTransaction().replace(R.id.settings_container, new MyPreferenceFragmentCompat()).commit();
但是,在CheckboxItem前面会插入一个大边距:
如何消除此边距或填充?如果使用的是AndroidX,则可以使用
首选项.seticonspacerserved(布尔iconspacerserved)
方法
因此,您将有:
item_Confirmation.setIconSpaceReserved(false);
您还可以检查答案。如果您使用的是AndroidX,您可以使用
首选项.setIConPasserved(布尔IConPasserved)
方法
因此,您将有:
item_Confirmation.setIconSpaceReserved(false);
您也可以检查答案