2012-01-09 56 views
4

我正在尝试使复选框变小。我曾尝试过使用XML中的布局和Java中的.width()/。height。也没有办法改变它的大小。我参加了向其他人推荐的教程,他问了这个问题,但我不明白他做了什么。有什么建议么?在android中更改复选框的大小

+0

你能一点点更具体?你想要多少? – Brian 2012-01-09 01:53:27

+0

在Facebook上记住您的用户名和密码的复选框的大小 – Aaron 2012-01-09 01:59:25

+0

可能重复的[Android:如何更改CheckBox大小?](http://stackoverflow.com/questions/2151241/android-how-to-change-checkbox大小) – Christoph 2012-10-26 13:00:51

回答

3

从另一个question这里SO:

你只需要设置相关的可绘制并设置他们中的复选框:

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

关键是如何设置可绘制的。 Here's a good tutorial about this

编辑:只是为了使它有点更清晰,你将需要这些文件,以使教程工作:

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。

+0

试图遵循本教程,添加他添加的文件,然后得到很多或错误 – Aaron 2012-01-09 03:14:47

+0

@Aaron:你能更具体地了解你收到的错误和你收到的代码吗? – Robert 2012-01-09 03:32:49

+0

确定我在drawables中添加了文件my_check_background。由于在my_checkbox_background开始引用其他文件(my_checkbox_background_on),所以更多的错误加剧了。每当我添加另一个文件更多的相同类型的错误弹出。 – Aaron 2012-01-09 04:15:17

1

一个简单的方法来做到这一点,从API级别11:

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

这不适用于我(Android 5)。 – CoolMind 2016-03-01 12:43:15

+1

如果您的复选框溢出之前的界限,这将简单地减少大小,不会照顾溢出。 – IcyFlame 2016-06-10 09:02:44