【问题标题】:Changing the size of a checkbox in android在android中更改复选框的大小
【发布时间】:2012-02-05 16:09:12
【问题描述】:

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

【问题讨论】:

标签: java android xml checkbox


【解决方案1】:

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

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

【讨论】:

  • 如果您的复选框之前溢出超出范围,这只会减小大小,不会处理溢出问题。
  • 我来这里是为了寻找一种简单的方法来扩大检查区域,这很好用
【解决方案2】:

来自另一个question,这里是:

你只需要设置相关的drawable并在checkbox里设置:

<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。

【讨论】:

  • 尝试按照本教程,添加他添加的文件,然后得到很多或错误
  • @Aaron:您能否更具体地说明您收到的错误以及收到错误的代码?
  • 当然我在drawables中添加了文件my_check_background。弹出更多错误,因为在 my_checkbox_background 开始引用其他文件(my_checkbox_background_on)。每次我添加另一个文件时,都会弹出更多相同类型的错误。
  • @Aaron:请参阅我对上述答案的编辑。我提供了我在 Eclipse 中的代码,该代码也在我的手机上进行了测试。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 2011-09-09
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 2012-04-24
相关资源
最近更新 更多