解决Preference中icon缺少时的空格

PreferenceFragmentCompat视图中会出现空格,这是由于icon未设置时引起的

values.xml中的iconSpaceReserved属性说明

<!-- Whether the space for the preference icon view will be reserved. If set to true, the
             preference will be offset as if it would have the icon and thus aligned with other
             preferences having icons. By default, preference icon view visibility will be set to
             GONE when there is no icon provided, so the default value of this attribute is false.
             -->
        <attr format="boolean" name="iconSpaceReserved"/>
        <attr name="android:iconSpaceReserved"/>

解决方法如下:

<EditTextPreference
            app:iconSpaceReserved="false" // icon未设置时,此次应设置为false,否则会出现空格
            app:key="real_name"
            app:title=“title"
            app:useSimpleSummaryProvider="true" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在SwitchPreference末尾添加图标,您可以使用以下步骤: 1. 创建一个新的PreferenceLayout资源文件,其包含一个ImageView和一个TextView,将它们放在一个LinearLayout。 2. 在您的SwitchPreference,设置您创建的新布局文件作为您的布局资源,并使用setWidgetLayoutResource()方法将其设置为您的SwitchPreference的widget布局。 3. 在SwitchPreference的onBindView()方法,获取ImageView并设置其图像资源,这样您的图标就会显示在SwitchPreference的末尾。 以下是一个示例代码: ```xml <!-- 新的PreferenceLayout资源文件 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Switch Preference with Icon" /> </LinearLayout> ``` ```java // 在您的SwitchPreference设置新布局文件作为widget布局 setWidgetLayoutResource(R.layout.preference_layout); @Override public void onBindView(View view) { super.onBindView(view); // 获取ImageView并设置其图像资源 ImageView iconView = (ImageView) view.findViewById(R.id.icon); iconView.setImageResource(R.drawable.your_icon_resource); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值