Xamarin Android:如何移除Preference标题与后续项的内边距及紫色间隙?
Got it, let's fix that annoying purple gap in your Preference screen to match Google Play Store's clean look! Since you're using Xamarin Android with Android.Support.V7.Preferences and PreferenceFragmentCompat, here are a few targeted solutions you can try:
1. Tweak Theme Color Properties (Quickest Fix)
That purple highlight/gap usually comes from the default colorControlHighlight or colorAccent in your Material theme. Google Play uses a nearly invisible (or brand-matching) selection state, so adjust these values in your custom theme:
Add/modify these entries in your styles.xml resource:
<style name="AppTheme.Preference" parent="@style/Theme.AppCompat.Light.DarkActionBar"> <!-- Replace with transparent or a color matching your background --> <item name="colorControlHighlight">#00000000</item> <!-- Optional: Match Play Store's green accent if needed --> <item name="colorAccent">#FF4CAF50</item> </style>
Then apply this theme to your PreferenceFragment in C#:
public override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Apply the custom preference theme SetStyle(Android.Support.V7.AppCompat.Fragment.StyleNormal, Resource.Style.AppTheme_Preference); AddPreferencesFromResource(Resource.Xml.preference_screen); }
2. Use a Custom Preference Layout (Full Control)
If theme adjustments don't get you the exact Play Store look, create a custom layout to eliminate any unwanted padding or background elements:
Create preference_custom.xml in your layout resources:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?android:attr/listPreferredItemHeightSmall" android:gravity="center_vertical" android:paddingStart="16dp" android:paddingEnd="16dp" android:background="?android:attr/selectableItemBackground"> <ImageView android:id="@android:id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="16dp"/> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical" android:paddingTop="16dp" android:paddingBottom="16dp"> <TextView android:id="@android:id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceListItem"/> <TextView android:id="@android:id/summary" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceListItemSecondary" android:maxLines="2" android:visibility="gone"/> </LinearLayout> <LinearLayout android:id="@android:id/widget_frame" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical"/> </LinearLayout>
Apply this layout either per-preference in your preference_screen.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <Preference android:key="example_pref" android:title="Example Preference" android:summary="Your summary text" android:layout="@layout/preference_custom"/> <!-- Repeat for other preferences --> </PreferenceScreen>
Or apply it globally via your theme:
<style name="AppTheme.Preference" parent="@style/Theme.AppCompat.Light.DarkActionBar"> <item name="preferenceStyle">@style/CustomPreferenceStyle</item> </style> <style name="CustomPreferenceStyle" parent="@style/Preference.AppCompat"> <item name="android:layout">@layout/preference_custom</item> </style>
3. Remove Default Dividers (If Needed)
Sometimes the "gap" you're seeing is actually the default preference divider. To match Play Store's divider-free look, add this to your PreferenceScreen XML:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:divider="@null" android:dividerHeight="0dp"> <!-- Your preferences go here --> </PreferenceScreen>
Start with the theme tweak first—it's the fastest solution. If you need pixel-perfect alignment with Google Play, the custom layout approach will give you full control.
内容的提问来源于stack exchange,提问作者pedzer

