2012-01-09 69 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