围绕复选框意见 [英] Centering CheckBox Views
问题描述
的如果你除了(或代替)的CheckBox感兴趣的单选按钮,请参见这个问题吧。的
If you're interested in RadioButtons in addition to (or instead of) CheckBoxes, see this question instead.
尽管在presence
Despite the presence of
<item name="android:layout_gravity">center_horizontal</item>
在样式文件中,两个复选框不居中,而是出现了左对齐。
in the style file, the two checkboxes are not centered, but appear "left-justified".
RES /布局/ activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/MyLL"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/cb1"
style="@style/CB_style" />
<CheckBox
android:id="@+id/cb2"
style="@style/CB_style" />
</LinearLayout>
RES /价值/ styles.xml
<style name="CB_style" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:layout_weight">1</item>
<item name="android:gravity">center</item>
<item name="android:checked">false</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
推荐答案
下面的东西,可以帮助:
Here's something that may help:
安卓重力VS机器人:layout_gravity
android:gravity vs android:layout_gravity
- 安卓重力通常是内部的,通常要求视图本身做什么用的像素有
- 的android:layout_gravity通常是外部的,通常是该视图不使用要求的ViewGroup父(LinearLayout中/ RelativeLayout的/的FrameLayout)如何使用额外的像素视图位置
如果您使用的是带有WRAP_CONTENT视图,您可能希望使用layout_gravity。如果您使用的是带有match_parent视图,你可能想尝试重力。
If you are using a view with wrap_content, you probably want to use layout_gravity. If you are using a view with match_parent, you probably want to try gravity.
有时包装另外的ViewGroup围绕一个麻烦的视图可以定位帮助。意见及ViewGroups经历一个复杂的屏幕空间的谈判阶段,不同的视图(按钮/ TextView中/ ImageView的/等)和ViewGroups(的LinearLayout / RelativeLayout的/ TableLayout /等)有不同的规则和谈判的权力
Sometimes wrapping another ViewGroup around a troublesome View can help with positioning. Views and ViewGroups go through an intricate "screen space" negotiating phase, and different Views (Buttons/TextView/ImageView/etc) and ViewGroups (LinearLayout/RelativeLayout/TableLayout/etc) have different rules and negotiating powers
这就是为什么有时候用配对像的FrameLayout或LinearLayout中另一个父麻烦视图可以使其行为所有的突然
This is why sometimes pairing a troublesome View with another parent like a FrameLayout or LinearLayout can make it behave all of the sudden
这篇关于围绕复选框意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!