在 Xamarin Android 应用程序中,我有一个 AppCompatRadioButton,其
drawableLeft
属性设置为 @drawable/person_resized
:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/MyRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selection 1"
android:textSize="24dp"
android:drawableLeft="@drawable/person_resized"
android:drawablePadding="5dp" />
</RadioGroup>
drawable/person_resized.xml
是一个图层列表可绘制对象,我用它来缩小 drawable/person.png
图像:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/person"
android:width="22dp"
android:height="22dp" />
</layer-list>
当此视图显示在 Android N 模拟器上时,它的行为正常。 但是在我的 Android Kitkat(4.4.4) 设备中,图像大小不正确。
这是我在 Android N 中得到的:
这是我在 Android Kitkat 中得到的:
这是怎么回事?
我尝试了以下方法来缩小 Android Kitkat 中的
drawableLeft
的大小,但 都不起作用:
使用ScaleDrawable缩小可绘制对象,同时确保SetLevel
从可绘制对象中获取位图,调整其大小并创建一个新的位图可绘制对象,但这会使图像消失
使用插入可绘制对象,与使用图层列表相同,使图像以其原始大小显示
Mahmoud Mortda指出的这个答案,Android Kitkat 中的一些功能障碍导致图层列表调整大小被忽略。
所以基本上你必须自己做,以下是我所做的:我创建了两个文件
layout-v21/radio_group.xml
和
layout/radio_group.xml
,前者将用于运行至少 Lollipop (SDK 21) 的 Android 设备,后者将用于包括 KitKat 在内的其余设备。
layout-v21/radio_group.xml
包含通过
RadioGroup
调整大小的图像的
layer-list
:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/MyRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selection 1"
android:textSize="24dp"
android:drawableLeft="@drawable/person_resized"
android:drawablePadding="5dp" />
</RadioGroup>
layout/radio_group.xml
包含一个
RadioGroup
,带有由
ImageView
和
TextView
制成的“手工制作”标签:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:id="@+id/RadGroup">
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/MyRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24dp" />
</RadioGroup>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_toRightOf="@+id/RadGroup"
android:layout_centerVertical="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon"
android:layout_width="22dp"
android:layout_height="22dp"
android:src="@drawable/person"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/icon"
android:layout_centerVertical="true"
android:textSize="24dp"
android:textColor="@color/black"
android:text="Selection 1"
/>
</RelativeLayout>
</RelativeLayout>
这里使用
ImageView
让我可以有效地调整图像大小。最后,在包含
RadioGroup
的 xml 布局中,只需包含
@layout/radio_group
,SDK 将加载针对 Android 版本的正确布局:
<include layout="@layout/radio_group"/>
<androidx.appcompat.widget.AppCompatRadioButton
android:id="@+id/dialog_pedido_item_produto_desconto_radio_button_desconto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/red_600"
android:bufferType="spannable"
android:gravity="center"
android:drawableStart="@drawable/ic_arrow_downward_red_24dp"
android:checked="true"
android:padding="@dimen/dp_5"
android:text="Desconto" />