改变android中复选框的大小

Aar*_*ron 5 java xml checkbox android

我正在尝试缩小复选框。我尝试过使用 XML 中的布局和 Java 中的 .width()/.height 。也没有任何东西可以改变它的大小。我去了向其他提出这个问题的人推荐的教程,但我不明白他做了什么。有什么建议么?

Luc*_*cas 5

从 API 级别 11 开始,一个简单的方法是:

<CheckBox
...
android:scaleX="0.50"
android:scaleY="0.50"
...
/>
Run Code Online (Sandbox Code Playgroud)


Rob*_*ert 4

来自另一个问题

你只需要设置相关的drawables并将它们设置在复选框中:

<CheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:text="new checkbox" 
    android:background="@drawable/my_checkbox_background"
    android:button="@drawable/my_checkbox" />
Run Code Online (Sandbox Code Playgroud)

诀窍在于如何设置可绘制对象。这是关于此的一个很好的教程

编辑:为了更清楚一点,您将需要这些文件才能使教程正常工作:

CheckBoxTestActivity.java:

import android.app.Activity;
import android.os.Bundle;

public class CheckBoxTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
Run Code Online (Sandbox Code Playgroud)

主要.xml:

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

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Checked CheckBox" 
        android:checked="true"/>

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Unchecked CheckBox" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/checkbox_background"
        android:button="@drawable/checkbox"
        android:text="New Checked CheckBox" 
        android:checked="true"/>

    <CheckBox
        android:id="@+id/checkBox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/checkbox_background"
        android:button="@drawable/checkbox"
        android:text="New Unchecked CheckBox" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

复选框.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/checkbox_off_background"/>

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

</selector>
Run Code Online (Sandbox Code Playgroud)

checkbox_background.xml:

<?xml version="1.0" encoding="utf-8"?>

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

    <item 
        android:drawable="@drawable/btn_check_label_background" />   

</selector>
Run Code Online (Sandbox Code Playgroud)

以及教程页面中的 btn_check_label_background.9.png、checkbox_off_background.png 和 checkbox_on_background.png。