如何并排放置4个视图,其中1个视图居中

时间:2017-06-13 10:03:02

标签: android xml

我正在尝试在活动顶部布置4个视图(2个按钮,2个TextViews)。我需要将button_2水平居中,TextView_2将剩余的空间占据视图的右侧,button_1指向wrap_content并固定在父级和textView_1的左侧以填充button_1和button_2之间的空间。 mock diagram to show what I mean

到目前为止我的代码:

 <LinearLayout
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:orientation="horizontal">
<Button
    android:id="@+id/button_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
<TextView
    android:id="@+id/textView_1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
<Button
    android:id="@+id/button_2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:layout_weight="1"/>
<TextView
    android:id="@+id/textView_2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>
</LinearLayout>

3 个答案:

答案 0 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">

<Button
    android:id="@+id/button_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:text="button_1"/>
<TextView
    android:id="@+id/textView_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_toRightOf="@+id/button_1"
    android:layout_toLeftOf="@+id/button_2"
    android:layout_alignBottom="@+id/button_2"
    android:text="textView_1"/>
<Button
    android:id="@+id/button_2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="button_2"/>
<TextView
    android:id="@+id/textView_2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="textView_2"
    android:gravity="center"
    android:layout_alignBottom="@+id/button_2"
    android:layout_alignParentEnd="true"
    android:layout_toEndOf="@+id/button_2" />

   </RelativeLayout>

答案 1 :(得分:1)

Relative Layout将完全适合您的情况。明智地使用android:layout_toRightOfandroid:layout_toLeftOfandroid:layout_alignParentRightandroid:layout_alignParentLeft属性。

<RelativeLayout
        android:layout_width="match_parent" android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"/>
        <TextView
            android:id="@+id/textView_1"
            android:layout_toRightOf="@+id/button_1"
            android:layout_toleftOf="@+id/button_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="hello there"/>
        <Button
            android:id="@+id/button_2"
            android:layout_width="wrap_content"
            android:layout_toRightOf="@+id/textView_1"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"/>
        <TextView
            android:id="@+id/textView_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/button_2"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>

答案 2 :(得分:0)

试试这个

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="1">
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_weight="1"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="1">
    <Button
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

删除子线性布局中的布局权重,更改高度以包装textview和按钮的内容以满足您的要求

相关问题