【问题标题】:How to resize the DrawableLeft of an AppCompatRadioButton in Android Kitkat?如何在 Android Kitkat 中调整 AppCompatRadioButton 的 DrawableLeft 大小?
【发布时间】:2018-03-11 21:31:32
【问题描述】:

在 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,但它们都不起作用:

【问题讨论】:

标签: android android-layout xamarin.android android-appcompat


【解决方案1】:

根据Mahmoud Mortda 指出的this answer,Android Kitkat 中的一些功能障碍导致层列表调整大小被忽略。

所以基本上你必须自己做,以下是我所做的:

我创建了两个文件layout-v21/radio_group.xmllayout/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,其中包含由ImageViewTextView 制成的“手工”标签:

<?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"/>

【讨论】:

    猜你喜欢
    • 2011-06-17
    • 2023-03-25
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多