ale*_*2k8 133
从API Level 11开始,存在另一种方法:
<CheckBox
...
android:scaleX="0.70"
android:scaleY="0.70"
/>
Run Code Online (Sandbox Code Playgroud)
Ant*_*lvy 82
这是一个更好的解决方案,不会剪切和/或模糊drawable,但只有当复选框本身没有文本时才有效(但你仍然可以有文本,它只是更复杂,最后看到).
<CheckBox
android:id="@+id/item_switch"
android:layout_width="160dp" <!-- This is the size you want -->
android:layout_height="160dp"
android:button="@null"
android:background="?android:attr/listChoiceIndicatorMultiple"/>
Run Code Online (Sandbox Code Playgroud)
结果:
什么以前的解决方案与scaleX和scaleY看起来像:
您可以通过TextView在其旁边添加一个文本复选框并在父布局上添加单击侦听器,然后以编程方式触发复选框.
mor*_*aes 56
您只需设置相关的drawable并在复选框中设置它们:
<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)
诀窍是如何设置drawables.这是一个很好的教程.
Aje*_*ary 11
好吧,我已经找到了很多答案,但是当我们需要文本以及我的UI中的复选框时,它们在没有文本的情况下工作正常
这里根据我的UI要求,我不能不增加TextSize所以我尝试的其他选项是scaleX和scaleY(Strach复选框)和自定义xml选择器与.png图像(它也创建不同屏幕大小的问题)
但我们有另一种解决方案,即Vector Drawable
分3步完成.
第1步:将Vector Drawable复制到您的drawable文件夹
checked.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16dp"
android:height="16dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.11,0 2,-0.9 2,-2L21,5c0,-1.1 -0.89,-2 -2,-2zM10,17l-5,-5 1.41,-1.41L10,14.17l7.59,-7.59L19,8l-9,9z" />
</vector>
Run Code Online (Sandbox Code Playgroud)
un_checked.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16dp"
android:height="16dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M19,5v14H5V5h14m0,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2z" />
</vector>
Run Code Online (Sandbox Code Playgroud)
(注意如果您正在使用Android Studio,您也可以在那里添加这些Vector Drawable表单,右键单击您的drawable文件夹,然后单击New/Vector Asset,从那里选择这些drawable) 步骤2:为check_box创建Xml选择器
check_box_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checked" android:state_checked="true" />
<item android:drawable="@drawable/un_checked" />
</selector>
Run Code Online (Sandbox Code Playgroud)
第3步:将drawable设置为复选框
<CheckBox
android:id="@+id/suggectionNeverAskAgainCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:button="@drawable/check_box_selector"
android:textColor="#FF000000"
android:textSize="13dp"
android:text=" Never show this alert again" />
Run Code Online (Sandbox Code Playgroud)
现在它像:
您也可以更改它的宽度和高度或viewportHeight和viewportWidth以及fillColor
希望它会有所帮助!
小智 9
我用
android:scaleX="0.70"
android:scaleY="0.70"
调整复选框的大小
然后我设置这样的边距
android:layout_marginLeft="-10dp"
Run Code Online (Sandbox Code Playgroud)
调整复选框的位置.
您可以尝试以下解决方案,通过在布局文件中custom checkbox设置以下属性来更改大小。Checkbox为我工作
机器人:scaleX =“0.8”机器人:scaleY =“0.8”
android:button="@null"
android:scaleX="0.8"
android:scaleY="0.8"
android:background="@drawable/custom_checkbox"
Run Code Online (Sandbox Code Playgroud)
将以下行添加到可绘制文件中
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/unchecked_img" />
<item android:state_checked="true"
android:drawable="@drawable/checked_img" />
</selector>
Run Code Online (Sandbox Code Playgroud)
这是我做的,第一集:
android:button="@null"
并设定
android:drawableLeft="@drawable/selector_you_defined_for_your_checkbox"
Run Code Online (Sandbox Code Playgroud)
然后在你的Java代码中:
Drawable d = mCheckBox.getCompoundDrawables()[0];
d.setBounds(0, 0, width_you_prefer, height_you_prefer);
mCheckBox.setCompoundDrawables(d, null, null, null);
Run Code Online (Sandbox Code Playgroud)
它对我有用,希望它对你有用!
| 归档时间: |
|
| 查看次数: |
112360 次 |
| 最近记录: |