【问题标题】:How to resize image in item selector xml如何在项目选择器xml中调整图像大小
【发布时间】:2018-01-29 16:20:27
【问题描述】:

如何在 XML 中调整选择器项的大小:

<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/check_box_2" />
    <item android:state_checked="true" android:drawable="@drawable/check_box_1" />
    <item android:drawable="@drawable/check_box_2" /> <!-- default state -->
</selector>

我的 XML 是:

<CheckBox
    android:text="llllll"
    android:button="@drawable/custom_checkbox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

【问题讨论】:

    标签: java android xml checkbox


    【解决方案1】:

    你可以用这种方式改变它:

    <item>
        <shape android:shape="rectangle">
            <size android:width="30" android:height="40"/>
            <solid android:color="#000000" />
        </shape>
    </item>
    

    从 API 级别 23 及更高版本开始,您实际上可以设置项目的宽度和高度

    <item
        android:drawable="@drawable/splash_logo"
        android:height="100dp"
        android:width="100dp" />
    

    注意:它是 android:width 而不是 android:layout_width

    编辑

    然而,还有另一种方法可以达到相同的目的,即相应地附加一个xml

    Checked.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android=http://schemas.android.com/apk/res/android
        android:shape="rectangle">
        <gradient 
            android:startColor="#ffff0000" 
            android:endColor="#ff000000" 
            android:angle="270" />
        <stroke 
            android:width="4px" 
            android:color="#ffc0c0c0" />
        <size 
            android:height="20dp" 
            android:width="20dp" />
    </shape> 
    

    然后将其附加为:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false" android:drawable="@xml/Checked" />
        <item android:state_checked="true" android:drawable="@drawable/check_box_1" />
        <item android:drawable="@drawable/check_box_2" /> <!-- default state -->
    </selector>
    

    请注意您的xml 文件中的已检查。您可以通过编辑来达到任何目的(相应地)

    【讨论】:

    • 我的应用使用 minSdkVersion =19
    • 请将您的xml 代码也嵌入到这些CheckBoxes 所在的位置。
    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多