【问题标题】:Android: Scale background image of RadioButton or another way to make a RadioButton an ImageAndroid:缩放 RadioButton 的背景图像或其他方式使 RadioButton 成为图像
【发布时间】:2018-05-24 07:45:22
【问题描述】:

我正在制作一个与纸牌游戏相关的应用,因此在整个应用中出现了许多纸牌图像。在一个部分中,我需要用户从四张卡片中选择一张,但只能选择一种,我希望卡片在被点击后稍微改变颜色,以表明它是当前选中的一张。

对于这个任务的大部分,带有 RadioButtons 的 RadioGroup 非常有意义,因为内置了选中/未选中状态以及在选择新选项时取消选择所有其他选项的功能;但是,问题是我希望按钮本身就是卡片,因此是没有任何文字的图像。

起初这篇帖子看起来很有希望:Adding custom radio buttons in android

我对这个策略的使用:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_weight="5"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/label_seatGod"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="8"
        android:gravity="center_vertical"
        android:text="@string/ex2"
        android:textColor="@color/font_condition_labels"
        android:textSize="@dimen/font_gen_label_size"
        android:textStyle="bold" />

    <RadioGroup
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="8" >

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@drawable/power_aphrodite"
            android:checked="false"
            android:text="@null" />
    </RadioGroup>

</LinearLayout>

导致:

这几乎是完美的。没有文本,可点击按钮空间的整个区域由我想要的图像组成,除了你可以看到图像根本没有缩放并且在边缘被裁剪。在 RadioButton 上方,您可以看到一个带有 4 个 ImageButton 的 LinearLayout,它准确地演示了我希望 RadioButton/card 图像如何显示(暂时忽略灰色,我还没有完成视图填充)。

基本上,我想要一个功能正常但看起来与顶部的四个 ImageButton 之一完全相同的 RadioButton。理想情况下,scaleType="fitCenter" 将是解决方法,因为它是我在示例 ImageButtons 中使用的,但您不能将该属性直接应用于 RadioButton 的可绘制按钮或其背景。

那么,如果不使用一些性能下降的布局陪审团索具或创建一个全新的 RadioButton 类,我如何才能使图像缩放而不被裁剪?

提前致谢。

【问题讨论】:

  • 是否可以选择使用矢量可绘制或每个 dpi 的不同资产?
  • 您可以尝试根据您的卡片对按钮的宽度和高度进行硬编码,使其不是方形的吗?
  • @deathangel908 - 我会考虑这一点,但我想避免这种简单的事情,特别是考虑到 ImageView 存在如此简单的方法。必须有一种方法可以将类似的功能与其他图像中的图像一起使用。有点傻,scaleType 不是大多数图像使用的属性。
  • @Suhaib Roomy - 这只是改变了裁剪的部分,根本不应用缩放。
  • 你按照图片保持了aspectRatio吗?

标签: java android scale android-imagebutton android-radiobutton


【解决方案1】:

也许您可以尝试在xml 中将其添加到您的RadioButton

    android:scaleType="centerCrop"

【讨论】:

  • 正如我在问题中所说,RadioButton 不像 ImageButton 那样具有该属性。
猜你喜欢
  • 1970-01-01
  • 2012-07-08
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 2012-09-24
  • 2015-07-15
  • 1970-01-01
  • 2011-10-14
相关资源
最近更新 更多