将填充应用于PreferenceFragment

时间:2018-03-12 22:38:48

标签: android android-layout android-view android-preferences preferenceactivity

我有一个SettingsActivity延长AppCompatPreferenceActivity

我像这样加载标题:

@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中的一个标题时,PreferenceFragment已加载getListView().setPadding()。此时内容位于操作栏下方。我需要再次拨打{{1}}。但是我怎么能这样做呢?我怎么知道碎片何时打开?

1 个答案:

答案 0 :(得分:3)

PreferenceFragment s&#39;内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());
        }

    }