在安卓中更改复选框的大小

5

我想把复选框变小。我已经尝试在XML中调整布局和在Java中使用.width()/.height(),但都没有改变它的大小。我去了推荐给其他人的教程,但我不理解他做了什么。有什么建议吗?


你能再具体一点吗?你想要多小? - Brian
Facebook上记住用户名和密码的复选框大小 - Aaron
可能是重复的问题:Android:如何更改CheckBox大小? - Christoph
2个回答

5

从API Level 11开始,一种简单的方法是:

<CheckBox
...
android:scaleX="0.50"
android:scaleY="0.50"
...
/>

1
如果您的复选框之前超出了边界,这只会减小其大小,而不会解决溢出问题。 - IcyFlame
1
我来这里寻找一种简单的方法来扩大检查区域,这个方法非常有效。 - Joel

4

以下是来自stackoverflow上的另一个问题

你只需要设置相关的可绘制对象并将它们设置在复选框中即可:

<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" />

“诀窍在于如何设置绘制对象。这里有一篇关于此的好教程:点此。”
“编辑:为了使教程更明确,您需要以下文件才能使教程正常运行:”
“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);
    }
}

main.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>

checkbox.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>

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>

从教程页面中获取btn_check_label_background.9.png、checkbox_off_background.png和checkbox_on_background.png。


尝试跟随这个教程,添加了他添加的文件,但是出现了很多错误。 - Aaron
@Aaron:你能具体说明一下你收到的错误以及出现错误的代码吗? - Robert
当然,我已经在drawables中添加了my_check_background文件。由于my_checkbox_background开始引用其他文件(my_checkbox_background_on),因此出现了更多的错误。每次我添加另一个文件时,都会出现相同类型的错误。 - Aaron
@Aaron:请看我对上面答案的修改。我提供了在Eclipse中的代码,也在我的手机上进行了测试。 - Robert

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接