簡體   English   中英

Android:縮放RadioButton的背景圖片或其他使RadioButton成為圖片的方法

[英]Android: Scale background image of RadioButton or another way to make a RadioButton an Image

我正在制作一個與紙牌游戲有關的應用程序,因此在整個應用程序中都會出現許多紙牌圖像。 在某一部分中,我需要用戶選擇四張卡中的一張,並且只能進行一張選擇,並且我想讓該卡在單擊以顯示它是當前選定的一張后略微改變顏色。

對於大多數任務而言,帶有RadioButtons的RadioGroup十分有意義,因為內置了具有選中/未選中狀態以及在選擇新選項時取消選擇所有其他選項的功能。 但是,問題是我希望按鈕本身就是卡片,因此希望圖像沒有文本。

起初,這篇文章看起來很有希望: 在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圖像如何顯示(現在忽略灰色,我還沒有完成填充視圖)。

本質上,我希望RadioButton能夠正常運行,但外觀與頂部的四個ImageButton之一完全一樣。 理想情況下,因為它是我在示例ImageButtons中使用的,所以scaleType =“ fitCenter”將是修復程序,但是您不能將該屬性直接應用於RadioButton的按鈕可繪制對象或其背景。

那么,如何在不依靠性能降低布局評審或創建全新RadioButton類的情況下縮放圖像而不進行裁剪呢?

提前致謝。

也許您可以嘗試將其添加到xmlRadioButton中:

    android:scaleType="centerCrop"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM