将填充应用于 PreferenceFragment

Apply padding to PreferenceFragment

我得到一个 SettingsActivity 扩展 AppCompatPreferenceActivity

我这样加载 headers:

@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onBuildHeaders(List<Header> target) {
    loadHeadersFromResource(R.xml.pref_headers, target);
}

pref_headers.xml:

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- These settings headers are only used on tablets. -->

    <header
        android:fragment="com.lifo.skipandgo.activities.SettingsActivity$GeneralPreferenceFragment"
        android:icon="@drawable/ic_info_black_24dp"
        android:title="@string/pref_header_general"/>

    <header
        android:fragment="com.lifo.skipandgo.activities.SettingsActivity$NotificationPreferenceFragment"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/pref_header_notifications" />

    <header
        android:fragment="com.lifo.skipandgo.activities.SettingsActivity$DataSyncPreferenceFragment"
        android:icon="@drawable/ic_sync_black_24dp"
        android:title="@string/pref_header_data_sync" />

</preference-headers>

现在我添加了一个 ActionBar 就像解释的那样 here:

private void setupActionBar() {
        getLayoutInflater().inflate(R.layout.pref_toolbar, (ViewGroup)findViewById(android.R.id.content));
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if (toolbar != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        int horizontalMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
        int verticalMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
        int topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (int) getResources().getDimension(R.dimen.activity_vertical_margin) + 30, getResources().getDisplayMetrics());
        getListView().setPadding(horizontalMargin, topMargin, horizontalMargin, verticalMargin);
    }

这很好用。但是,当我单击 Activity 中的 headers 之一时,会加载 PreferenceFragment。此时内容位于操作栏下方。我需要再次致电 getListView().setPadding()。但是我该怎么做呢?我如何知道片段何时打开?

在您的 PreferenceFragments' onViewCreated() 中执行以下操作:


    public class MyPreferenceFragment extends PreferenceFragment {

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            // acquire padding side just like you have done in activity
            int topPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (int) getResources().getDimension(R.dimen.activity_vertical_margin) + 30, getResources().getDisplayMetrics());

            // do not change current paddings of start, end, and bottom
            // increase top padding by precomputed pixels
            view.setPadding(view.getPaddingStart(), view.getPaddingTop() + topPadding, view.getPaddingEnd(), view.getPaddingBottom());
        }

    }