[英]Android: How to adjust Margin/Padding in Preference?
在我要求刪除 PreferenceActivity/PreferenceFragment 的填充之前:
Android:如何最大化 PreferenceFragment 寬度(或去掉邊距)?
這次我在標題文本之前無法調整邊距/填充。 (見下圖)。
有人有任何想法嗎?
您可以像這樣自定義復選框:MyPreference.xml
<CheckBoxPreference android:key="testcheckbox"
android:title="Checkbox Title"
android:summary="Checkbox Summary..."
android:layout="@layout/custom_checkbox_preference_layout">
</CheckBoxPreference>
和 custom_checkbox_preference_layout.xml
<?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/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingLeft="16dip"
android:paddingRight="?android:attr/scrollbarSize">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:minWidth="0dp"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<TextView android:id="@+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="@+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@android:id/title"
android:layout_alignLeft="@android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:maxLines="4" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="@+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" />
</LinearLayout>
重點是 android:minWidth="0dp"。
對於任何面臨此問題且不想進行自定義布局的人。 我想指出的是,添加:
android:icon="@null"
解決了我的左側空白空間問題。
<PreferenceScreen android:icon="@null" android:title="Your title" android:summary="Your summary" />
截至 2020 年 2 月 16 日,此空間用於圖標。 要擺脫這個空間,請使用:
爪哇
preference.setIconSpaceReserved(false);
XML
<androidx.preference.PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<Preference
...
app:iconSpaceReserved="true/false"
.../>
</androidx.preference.PreferenceScreen>
True 添加填充。 False 刪除它。
PS:使用的依賴
implementation 'androidx.preference:preference:1.1.0'
user1482130 描述的 custom_checkbox_preference_layout.xml 也無需修改其他類型的首選項,例如 listpreferences ! 非常有用
android:layout="@layout/custom_checkbox_preference_layout">
如果您要動態創建首選項,請將 ContextThemeWrapper 作為參數傳遞給新的首選項
TypedValue themeTypedValue = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.preferenceTheme, themeTypedValue, true);
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getActivity(), themeTypedValue.resourceId);
Preference preference = new Preference(contextThemeWrapper);
我在這篇文章中找到了它: Dynamically created Preference Screens on Android
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.