【问题标题】:PreferenceFragmentCompat adds margin to preference itemsPreferenceFragmentCompat 为首选项添加边距
【发布时间】:2019-11-21 08:22:51
【问题描述】:

考虑这个例子:

在 android 中有一个最小的 Activity 将这个布局作为 root 膨胀:

<!-- 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>

在activity的onCreate中:

setContentView(R.layout.activity_preference);

然后我想用我的PreferenceFragmentCompat 替换 settings_container。 我正在使用当前的 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);
    }

以下是活动如何处理我的片段:

getSupportFragmentManager().beginTransaction().replace(R.id.settings_container, new MyPreferenceFragmentCompat()).commit();

但是在 CheckboxItem 前面插入了一个宽边距:

如何消除此边距或内边距?

【问题讨论】:

    标签: android androidx preferencefragment


    【解决方案1】:

    如果您使用的是AndroidX,则可以使用Preference.setIconSpaceReserved(boolean iconSpaceReserved) 方法。

    所以,你将拥有:

    item_Confirmation.setIconSpaceReserved(false);
    

    您也可以查看this的答案。

    【讨论】:

      猜你喜欢
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-28
      相关资源
      最近更新 更多