AppCompatPreferenceActivity 在 Android 4.4 上左右出现奇怪的内边距

11

我有一个继承了AppCompatPreferenceActivity的SettingsActivity。

我的 pref_headers.xml 看起来像这样:

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
    <header
        android:fragment="com.blabla.activities.fragments.ProfileFragment"
        android:icon="@drawable/ic_users"
        android:title="Profil">
    </header>
</preference-headers>

代码片段如下:

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class ProfileFragment extends PreferenceFragment {
    @BindView(R.id.email)
    TextView email;
    @BindView(R.id.username)
    TextView username;
    @BindView(R.id.loadingPanel)
    RelativeLayout loadingPanel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_profile, container, false);
        ButterKnife.bind(this, rootView);

        if (setActionbarListener != null) {
            setActionbarListener.setActionbarTitle("Profil");
        }

        loadingPanel.setVisibility(View.VISIBLE);
        UsernameHandler uHandler = UsernameHandler.getInstance(new UsernameResult() {
            @Override
            public void finished(String uname) {
                username.setText(uname);
                loadingPanel.setVisibility(View.GONE);  // hide loading spinner
            }
        });
        email.setText(MainActivity.getFbUser().getEmail());

        return rootView;
    }
}

我的问题是,我加载的布局左右两侧有些空白。这个内边距明显不是来自于布局本身。在Android 8.1中启动应用程序时,没有这样的内边距/外边距。

请看图片:

输入图片描述

最后是SettingsActivity的代码:

public class SettingsActivity extends AppCompatPreferenceActivity implements OnSetActionbarTitleListener {

    /**
     * A preference value change listener that updates the preference's summary
     * to reflect its new value.
     */
    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
            String stringValue = value.toString();

            if (preference instanceof ListPreference) {
                // For list preferences, look up the correct display value in
                // the preference's 'entries' list.
                ListPreference listPreference = (ListPreference) preference;
                int index = listPreference.findIndexOfValue(stringValue);

                // Set the summary to reflect the new value.
                preference.setSummary(
                        index >= 0
                                ? listPreference.getEntries()[index]
                                : null);

            } else if (preference instanceof RingtonePreference) {
                // For ringtone preferences, look up the correct display value
                // using RingtoneManager.
                if (TextUtils.isEmpty(stringValue)) {
                    // Empty values correspond to 'silent' (no ringtone).
                    preference.setSummary(R.string.pref_ringtone_silent);

                } else {
                    Ringtone ringtone = RingtoneManager.getRingtone(
                            preference.getContext(), Uri.parse(stringValue));

                    if (ringtone == null) {
                        // Clear the summary if there was a lookup error.
                        preference.setSummary(null);
                    } else {
                        // Set the summary to reflect the new ringtone display
                        // name.
                        String name = ringtone.getTitle(preference.getContext());
                        preference.setSummary(name);
                    }
                }

            } else {
                // For all other preferences, set the summary to the value's
                // simple string representation.
                preference.setSummary(stringValue);
            }
            return true;
        }
    };

    /**
     * Helper method to determine if the device has an extra-large screen. For
     * example, 10" tablets are extra-large.
     */
    private static boolean isXLargeTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
    }

    /**
     * Binds a preference's summary to its value. More specifically, when the
     * preference's value is changed, its summary (line of text below the
     * preference title) is updated to reflect the value. The summary is also
     * immediately updated upon calling this method. The exact display format is
     * dependent on the type of preference.
     *
     * @see #sBindPreferenceSummaryToValueListener
     */
    public static void bindPreferenceSummaryToValue(Preference preference) {
        // Set the listener to watch for value changes.
        preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);

        // Trigger the listener immediately with the preference's
        // current value.
        sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
                PreferenceManager
                        .getDefaultSharedPreferences(preference.getContext())
                        .getString(preference.getKey(), ""));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupActionBar();
    }


    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    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);
        }

        setActionBarBelowContent();
    }

    private void setActionBarBelowContent() {
        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 actionBarHeight = UserInterfaceUtils.getActionBarHeight(MainActivity.context);
        getListView().setPadding(0, actionBarHeight+10, 0, verticalMargin);
    }

    @Override
    public void onHeaderClick(Header header, int position) {
        setActionBarBelowContent();
        super.onHeaderClick(header, position);
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean onIsMultiPane() {
        return isXLargeTablet(this);
    }

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

    /**
     * This method stops fragment injection in malicious applications.
     * Make sure to deny any unknown fragments here.
     */
    @Override
    protected boolean isValidFragment(String fragmentName) {
        return PreferenceFragment.class.getName().equals(fragmentName)
                || SettingsFragment.class.getName().equals(fragmentName)
                || FeedbackFragment.class.getName().equals(fragmentName)
                || ProfileFragment.class.getName().equals(fragmentName)
                || AGBFragment.class.getName().equals(fragmentName)
                || PrivacyFragment.class.getName().equals(fragmentName)
                || FAQFragment.class.getName().equals(fragmentName)
                || LicenseFragment.class.getName().equals(fragmentName);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        setActionbarTitle("Einstellungen");
    }

    @Override
    public void setActionbarTitle(String title) {
        getSupportActionBar().setTitle(title);
    }
}

编辑 - 添加布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/item_list_item"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardUseCompatPadding="true"
        app:cardElevation="4dp"
        app:cardCornerRadius="0dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp"
            android:padding="15dp"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/preUsername"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Dein Benutzername:"
                    android:textAppearance="@style/profileText"
                    android:textStyle="bold"/>

                <TextView
                    android:id="@+id/username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/profileText"
                    android:layout_marginLeft="5dp"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/preEmail"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/profileText"
                    android:textStyle="bold"
                    android:text="Deine Email:"/>

                <TextView
                    android:id="@+id/email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textAppearance="@style/profileText"
                    android:layout_marginLeft="5dp"/>

            </LinearLayout>

        </LinearLayout>
    </android.support.v7.widget.CardView>

    <RelativeLayout
        android:id="@+id/loadingPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center">

        <ProgressBar
            android:id="@+id/progressBar"
            style="@style/MyProgressBarSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="true" />
    </RelativeLayout>

</RelativeLayout>

你能同时发布你的布局吗? - MRah
@MRah 我已将其添加到问题中,但正如我所提到的,我认为这与片段本身的布局无关。 - progNewbie
这是来自您的卡片视图布局的卡片兼容填充。 - MRah
看起来是从Activity布局。 - Marcos Vasconcelos
2
这些事情在不同的API级别之间有时可能会变化。我认为你的问题源于API版本之间的布局差异。你能否发布一个简单的演示应用程序来帮助追踪问题?如果你这样做,我相信你会很快得到答案。 - Cheticamp
显示剩余4条评论
5个回答

3
根据设计,即使在KitKat设备上,你也会得到padding,这是预期行为。从官方文档可以看到:

在Lollipop之前的平台上,CardView不会剪辑圆角的边界。相反,它会向内容添加填充,以避免重叠于圆角处。您可以通过将此字段设置为false来禁用此行为。

解决方案: 在XML布局中:app:cardPreventCornerOverlap="false" 或者
在Java中:setPreventCornerOverlap(false); 因此,在你的情况下:
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardUseCompatPadding="true"
    app:cardElevation="4dp"
    app:cardPreventCornerOverlap="false"
    app:cardCornerRadius="0dp">

希望这能对您有所帮助!

很遗憾,这并没有解决我的问题 :( 即使我不使用卡片视图,我仍然遇到了这个问题。 - progNewbie

2

你说得对,这个填充不是因为 CardView

enter image description here

这是因为在 pre-lolipop 中 prefs 的填充造成的:

enter image description here

enter image description here

为了解决这个问题,您可以在创建视图后从父视图中删除填充。

 @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View containerParent = (View)view.getParent();
        containerParent.setPadding(0, 0, 0, 0);
    }

这将删除早期安卓版本上的内边距。

enter image description here


0

如果您正在使用app:cardUseCompatPadding=true,请使用负的layout_margin来平衡兼容库中不需要的填充。以下是示例:

<android.support.v7.widget.CardView

   android:layout_width="match_parent"
   android:layout_margin="-10dp"
   android:layout_height="wrap_content"
   app:cardUseCompatPadding="true"
   app:cardElevation="4dp"
   app:cardCornerRadius="0dp">

// Your other views

</android.support.v7.widget.CardView>

这将删除不必要的空格,并在所有操作系统上看起来类似(未在Orio中测试)

有关CardView的更详细信息,请参阅{{link1:Android CardView}}文档。还可以检查此stackoverflow答案{{link2:Android layout unwanted padding}}


正如我在评论和另一个答案中所述,这个问题与CardView的任何填充都无关。 - progNewbie

0

我记得我在处理Preference时遇到过类似的问题。我想我使用了:

if(view != null) {
    ListView lv = (ListView) view.findViewById(android.R.id.list);
    lv.setPadding(0, 0, 0, 0);
}

onCreateView()中。


你是指SettingsActivity中的onCreate吗?那么视图从哪里来?我认为这并不能解决我的问题,因为你试图改变Settingsactivity中初始ListView的填充。但是我已经在我的自己的Fragment中了。 - progNewbie

0
你是否尝试过在扩展ProfileFragment时使用PreferenceFragmentCompat,类似于:public class ProfileFragment extends PreferenceFragmentCompat? 看起来PreferenceFragment已经过时了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接