Android Studio | How do I scale the image in ToggleButton?

倖福魔咒の 提交于 2019-12-14 02:21:52

问题


I have implemented ToggleButton to use image instead of text but how do I scale them

I have tried using scaleType but not work

ic_toggle.xml (I have tried using scaleType here too but not work)

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="false"
          android:drawable="@drawable/ic_signal_p1"/>

    <item android:state_checked="true"
          android:drawable="@drawable/ic_signal_p2" />

</selector>

fragment_deviceitem_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:orientation="vertical">

    <ToggleButton
        android:id="@+id/scan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_toggle"
        android:scaleType="fitCenter"
        android:layout_margin="10dp"
        android:textOn=""
        android:textOff=""
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_centerVertical="true"/>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="63dp"
        android:layout_alignParentBottom="true"
        android:visibility="visible" />

</RelativeLayout>

Original Image

Here's result I got.


回答1:


try this

<ToggleButton
    android:id="@+id/scan"
     android:layout_width="30px"
     android:layout_height="30px"
    android:background="@drawable/ic_toggle"
    android:scaleType="fitCenter"
    android:layout_margin="10dp"
    android:textOn=""
    android:textOff=""
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:layout_centerVertical="true"/>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:visibility="visible" />



来源:https://stackoverflow.com/questions/43273986/android-studio-how-do-i-scale-the-image-in-togglebutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!