在我要求移除PreferenceActivity/PreferenceFragment的填充之前:
Android:如何最大化PreferenceFragment宽度(或去掉边距)?
这一次,我在调整标题文本之前的边距/填充方面遇到了麻烦。(见下图)
有人有任何想法吗?
在我要求移除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" />
androidx.preference:preference:1.0.0
或更高版本,则无法正常工作。 - Joonsooapp:iconSpaceReserved="false"
。 - coder 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'
com.android.support:preference-v7:28.0.0
- Rodgers Andati由user1482130描述的custom_checkbox_preference_layout.xml也可用于其他类型的首选项,例如listpreferences!非常有用
android:layout="@layout/custom_checkbox_preference_layout">
TypedValue themeTypedValue = new TypedValue();
getActivity().getTheme().resolveAttribute(R.attr.preferenceTheme, themeTypedValue, true);
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(getActivity(), themeTypedValue.resourceId);
Preference preference = new Preference(contextThemeWrapper);
我在这篇文章中找到了它:动态创建Android上的首选项屏幕
android:icon
。你可以尝试提供自己的drawable,并设置尽可能小的尺寸,看看是否有所帮助。 - Stan