如何在偏好之间设置space

How to set space between preferences

我是新开发者。偏好之间的space很大,不好看。我有一个定义的首选项布局文件。 如何将它们设置得更近?非常感谢!

preferences.xml

<PreferenceCategory
  android:title="@string/title_user_profile"
  android:key = "@string/preference_key_category_usersetting"
  android:layout="@layout/preference_layout">

  <Preference android:title="@string/user_email_address"
  android:key="@string/preference_key_email"
  android:summary="@string/summary_joined_from"
  android:editable="false"
  android:clickable="false"
  android:lineSpacingExtra="-4dp"/>

  <com.ipretii.app.activity.setting.NamePreference
  android:key="@string/preference_key_username"
  android:summary="Type in your user name"
  android:lineSpacingExtra="-4dp"/>

  <com.ipretii.app.activity.setting.BirthYearPickerPreference
  android:title="Birth Year"
  android:key="@string/preference_key_birthyear"
  android:lineSpacingExtra="-4dp"/>

  </PreferenceCategory>

preference_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView android:id="@+android:id/title"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="@dimen/text_size_title"
  android:textColor="@color/colorPrimary"
  android:paddingLeft="16dp"
  android:paddingRight="16dp"
  android:paddingTop="16dp"/>

  <TextView android:id="@android:id/summary"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="@dimen/text_size_subheading"
  android:textColor="@color/colorSubHeadingText"
  android:paddingRight="16dp"/>

  </LinearLayout>

我认为您可以根据自己的喜好调整 android:padding 属性。

例如,如果你想将它们垂直设置得更近,你可以将 android:paddingTop 设置为 8dp 或其他。

虽然我不能声称喜欢这个解决方案,但我确实使用以下代码解决了这个问题。我使用 DialogPreference、CheckBoxPreference 和 ListPreference 对此进行了测试。我没有尝试使用自定义布局的任何首选项。

在我的 SettingsFragment 中,我创建了一个 class 来包装一个 ListAdapter 并将其所有子级的垂直填充设置为 0:

// An Adapter that wraps another adapter, but updates all child Views to have zero vertical padding
public static class ListAdapterWrapper implements ListAdapter
{
    private ListAdapter wrappedAdapter;

    public ListAdapterWrapper(ListAdapter wrappedAdapter)
    {
        super();
        this.wrappedAdapter = wrappedAdapter;
    }

    @Override
    public android.view.View getView(int i, android.view.View view, android.view.ViewGroup viewGroup)
    {
        View newView = wrappedAdapter.getView(i, view, viewGroup);
        if (newView != null && newView instanceof LinearLayout)
        {
            LinearLayout layout = (LinearLayout)newView;

            // set the top and bottom padding to 0 for each child
            int count = layout.getChildCount();
            for(int ii=0; ii<count; ii++)
            {
                View v = layout.getChildAt(ii);
                v.setPadding(v.getPaddingLeft(), 0, v.getPaddingRight(), 0);
            }
        }

        return newView;
    }

    @Override public boolean areAllItemsEnabled() { return wrappedAdapter.areAllItemsEnabled(); }
    @Override public boolean isEnabled(int i) { return wrappedAdapter.isEnabled(i); }
    @Override public void registerDataSetObserver(android.database.DataSetObserver dataSetObserver) { wrappedAdapter.registerDataSetObserver(dataSetObserver); }
    @Override public void unregisterDataSetObserver(android.database.DataSetObserver dataSetObserver) { wrappedAdapter.unregisterDataSetObserver(dataSetObserver); }
    @Override public int getCount() { return wrappedAdapter.getCount(); }
    @Override public java.lang.Object getItem(int i) { return wrappedAdapter.getItem(i); }
    @Override public long getItemId(int i) { return wrappedAdapter.getItemId(i); }
    @Override public boolean hasStableIds() { return wrappedAdapter.hasStableIds(); }
    @Override public int getItemViewType(int i) { return wrappedAdapter.getItemViewType(i); }
    @Override public int getViewTypeCount(){ return wrappedAdapter.getViewTypeCount(); }
    @Override public boolean isEmpty() { return wrappedAdapter.isEmpty(); }
}

然后我用 ListAdapterWrapper 覆盖了 ListView 的适配器:

    @Override
    public void onActivityCreated(android.os.Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        ListView list = (ListView)getView().findViewById(android.R.id.list);
        if (list != null)
        {
            // Overwrite the adapter so we can decrease the padding between Settings items
            list.setAdapter(new ListAdapterWrapper(list.getAdapter()));
        }
    }

只需放置一个简单的禁用 Preference 标签即可:

    <Preference
        android:enabled="false"
        app:title="" />