Android 将视图内容居中放置在线性布局中
下面是布局图。我试图重新定位复选框的位置;将其移动到视图的右侧。android:gravity和android:layou-gravity似乎没有效果。有什么解释吗?此线性布局是相对布局的子级Android 将视图内容居中放置在线性布局中,android,layout,Android,Layout,下面是布局图。我试图重新定位复选框的位置;将其移动到视图的右侧。android:gravity和android:layou-gravity似乎没有效果。有什么解释吗?此线性布局是相对布局的子级 <LinearLayout android:id="@+id/deviceLinear" android:layout_width="fill_parent" android:layout_height="match_paren
<LinearLayout
android:id="@+id/deviceLinear"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_below="@id/view1" >
<ImageView
android:contentDescription="@string/devices_icon"
android:id="@+id/devices_img"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.15"
android:layout_gravity="center"
android:src="@drawable/hardware_phone" />
<TextView
android:id="@+id/devices"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.43"
android:text="@string/devices"
android:textSize="20sp" />
<CheckBox
android:id="@+id/check_devices"
android:button="@drawable/custom_checkbox"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.42"
android:onClick="onCheckboxClicked" />
</LinearLayout>
我假设您希望
复选框位于线性布局的右侧。因为你给了它一个layout\u权重
,所以不管它的大小,它都会占据一定的宽度百分比,正如你从蓝色边界框中看到的那样
尝试将复选框包装到另一个具有android:orientation='horizontal'
的LinearLayout
中。将0.42
重量赋予包装LinearLayout
,然后将LinearLayout
的android:gravity
设置为right
。将复选框移动到最右侧时,应保持间距
大概是这样的:
<LinearLayout
android:orientation="horizontal"
android:layout_width="0dp"
android:layout_weight="0.42"
android:layout_height="wrap_content"
android:gravity="right" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
您可能还需要考虑一个<代码> RealValayOuts,它允许您基于其他视图的位置来定位视图。
< p>您将LayOutoSuth=“0.42”赋予您的复选框,这意味着它将以线性布局的宽度为基础来测量宽度。通过这种方法,你将永远无法把它放在正确的位置
实现目标的最好方法是使用相对论。这是我的列表项布局,右边有一个复选框。很抱歉,合并标记已与合并在RelativeLayout中
android:layout_width="match_parent"
android:layout_height="wrap_content"
属性
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/imgChannelIcon"
android:layout_alignParentLeft="true"
style="?muListItemImage60x60" />
<CheckBox android:id="@+id/chkIsChecked"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_alignTop="@+id/imgChannelIcon"
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/imgChannelIcon"
android:layout_marginLeft="@dimen/list_item_checkbox_margin_left"
android:layout_marginRight="@dimen/list_item_checkbox_margin_right"
android:layout_marginTop="@dimen/list_item_checkbox_margin_top"
android:layout_marginBottom="@dimen/list_item_checkbox_margin_bottom"
android:focusable="false"
android:focusableInTouchMode="false" />
<TextView android:id="@+id/lblChannelTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imgChannelIcon"
android:layout_alignTop="@+id/imgChannelIcon"
android:layout_toLeftOf="@+id/chkIsChecked"
android:layout_alignWithParentIfMissing="true"
android:includeFontPadding="false"
android:textStyle="bold"
android:text="@string/channel_item_dummy_title"
style="?muListItemTextBig" />
<TextView android:id="@+id/lblChannelSubtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imgChannelIcon"
android:layout_below="@+id/lblChannelTitle"
android:layout_toLeftOf="@+id/chkIsChecked"
android:layout_alignWithParentIfMissing="true"
android:includeFontPadding="false"
android:textStyle="normal"
android:text="@string/channel_item_dummy_subtitle"
style="?muListItemTextNormal" />
<TextView android:id="@+id/lblChannelCategory"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toRightOf="@+id/imgChannelIcon"
android:layout_below="@+id/lblChannelSubtitle"
android:layout_alignBottom="@+id/imgChannelIcon"
android:layout_toLeftOf="@+id/chkIsChecked"
android:layout_alignWithParentIfMissing="true"
android:includeFontPadding="false"
android:paddingLeft="3dp"
android:gravity="center_vertical|left"
android:textStyle="italic"
android:text="@string/channel_item_dummy_category"
style="?muListItemTextNormal_Inverse" />
<TextView android:id="@+id/lblChannelUpdate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@+id/imgChannelIcon"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:includeFontPadding="false"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:textStyle="italic"
android:text="@string/channel_item_dummy_update"
style="?muListItemTextTiny" />
</merge>
如您所见,我首先使用layout\u alignParentLeft=“true”放置图像,然后使用layout\u alignParentRight=“true”放置复选框,最后根据这两个选项排列其他组件。从您的图像中,我可以看到您可以将设备img用作左侧组件(但您必须给它一个固定的高度,也许还需要一些边距才能成为布局的高度),并将您的复选框用作右侧组件
请记住,在将wrap_内容作为高度的RelativeLayout中,不能使用AlignParentBottom属性
希望这有帮助…您的布局文件显示,无论是线性布局还是复选框项,都没有对复选框应用重力。看起来您可能错误地将手机图像的gravity放在ImageView上。我尝试了它,但删除了它,因为它没有做任何事情。尝试使用android:gravity=“right”
整个android:gravity库似乎无法与正常工作的复选框一起正常工作。为什么android:gravity=“center”
可以处理文本视图和图像,但不能处理复选框?gravity
适用于其所在元素中的项目。因此,对于布局,它适用于布局中的内容<代码>布局\u重力
将视图定位在其包含的元素中。因此,我上面写的内容可以在LinearLayout
上切换为nogravity
,在复选框上切换为layout\u gravity=right
。