Android AppCompatCheckbox padding/margin/gravity
Android AppCompatCheckbox padding/margin/gravity
我的布局中有一个 AppCompatCheckBox。这是它的样子:
问题是填充或边距或重力不是我想要的方式。 Box 和左边框之间有一个小间隙。我将 gravity 设置为 left/start padding 并将 margin 设置为 0,但差距仍然存在。我怎样才能删除它?我希望它完全位于左边界或居中。两者都会好起来的。但是将重力设置为中心也不起作用。
有人有想法吗?
CheckBox 和 Text 应该在彼此之上。
<LinearLayout
android:id="@+id/llRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/llCbs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="?attr/textColor" />
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="?attr/textColor" />
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="?attr/textColor" />
</LinearLayout>
<LinearLayout
android:id="@+id/llTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
确保容器布局没有应用边距
看看这个 http://android4beginners.com/2013/07/lesson-2-2-how-to-use-margins-and-paddings-in-android-layout/
您只需添加 linearlayout 并将其重心设置为居中,然后将 AppCompatCheckbox 添加为其子项,即可将 AppCompatCheckbox 设置在中心。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
不是 AppCompatCheckBox 的问题,而是 CheckBox 本身的问题。我找到的唯一解决方案是设置负边距(对我来说 -7dp 很棒)。
将复选框 minWidth
和 minHeight
的值设置为零
checkbox.setMinWidth(0);
checkbox.setMinHeight(0);
或通过xml
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
android:minHeight="0dp" />
我的布局中有一个 AppCompatCheckBox。这是它的样子:
问题是填充或边距或重力不是我想要的方式。 Box 和左边框之间有一个小间隙。我将 gravity 设置为 left/start padding 并将 margin 设置为 0,但差距仍然存在。我怎样才能删除它?我希望它完全位于左边界或居中。两者都会好起来的。但是将重力设置为中心也不起作用。
有人有想法吗?
CheckBox 和 Text 应该在彼此之上。
<LinearLayout
android:id="@+id/llRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="@+id/llCbs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="?attr/textColor" />
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="?attr/textColor" />
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="?attr/textColor" />
</LinearLayout>
<LinearLayout
android:id="@+id/llTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
确保容器布局没有应用边距
看看这个 http://android4beginners.com/2013/07/lesson-2-2-how-to-use-margins-and-paddings-in-android-layout/
您只需添加 linearlayout 并将其重心设置为居中,然后将 AppCompatCheckbox 添加为其子项,即可将 AppCompatCheckbox 设置在中心。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
不是 AppCompatCheckBox 的问题,而是 CheckBox 本身的问题。我找到的唯一解决方案是设置负边距(对我来说 -7dp 很棒)。
将复选框 minWidth
和 minHeight
的值设置为零
checkbox.setMinWidth(0);
checkbox.setMinHeight(0);
或通过xml
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
android:minHeight="0dp" />