我有以下布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_weight="1">


        <WebView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/webview" android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

    <LinearLayout android:orientation="horizontal"  android:layout_width="fill_parent" android:layout_height="fill_parent"  android:layout_weight="13">
        <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
                    <ImageButton android:background="@null" android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/back" android:padding="10dip" />
            </LinearLayout>

            <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
                <ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" />
            </LinearLayout>

        </LinearLayout>

        <RelativeLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent"    android:layout_weight="1" >
                <ImageButton android:background="@null" android:id="@+id/special"   android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_gravity="right"/>
        </RelativeLayout>




    </LinearLayout>


</LinearLayout>

出于这个问题的目的,我只关心布局的下半部分。现在它包含3个ImageButton。第一个2,我想彼此左右对齐。第三个,我想对齐右侧。

像以前一样,前两个按钮是我希望它们成为的地方,但是第三个按钮保持左侧。我将如何使其对齐。

有帮助吗?

解决方案

布局效率极低且肿。你不需要那么多 LinearLayouts。实际上,您不需要 LinearLayout 根本。

仅使用一个 RelativeLayout. 。像这样。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageButton android:background="@null"
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/back"
        android:padding="10dip"
        android:layout_alignParentLeft="true"/>
    <ImageButton android:background="@null"
        android:id="@+id/forward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/forward"
        android:padding="10dip"
        android:layout_toRightOf="@id/back"/>
    <ImageButton android:background="@null"
        android:id="@+id/special"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/barcode"
        android:padding="10dip"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

其他提示

您只需使用一个就可以做到这一切 RelativeLayout (顺便说一句,不需要 android:orientation 范围)。因此,而不是拥有 LinearLayout, ,包含一堆东西,您可以做类似的事情:

<RelativeLayout>
    <ImageButton
        android:layout_width="wrap_content"
        android:id="@+id/the_first_one"
        android:layout_alignParentLeft="true"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/the_first_one"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

正如您注意到的那样,缺少一些XML参数。我只是展示了您必须放置的基本参数。您可以完成其余的。

如果您想使用 线层, ,您可以与 layout_weightSpace 元素。

例如以下布局位置 textViewtextView2 彼此相邻 textView3 将正确对准

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView2" />

    <Space
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="20dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView3" />
</LinearLayout>

您可以在没有的情况下达到相同的效果 Space 如果您要设置 layout_weighttextView2. 。只是我更喜欢事物更分开,加上展示 Space 元素。

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView2" />

注意 您应该(虽然不得必须)设置 layout_width 明确地将其根据重量重新计算(无论如何都应重新计算) LinearLayout)。有关其他布局性能提示 Android布局技巧 系列。

这是一个示例 Relativelayout:

RelativeLayout relativeLayout=(RelativeLayout)vi.findViewById(R.id.RelativeLayoutLeft);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
                params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                relativeLayout.setLayoutParams(params);

使用另一种布局(示例Linearlayout),您只需要更改 Relativelayout 为了 线层.

为了支持旧版本的空间,可以用如下视图替换。在大多数组件之后和右侧大多数组件之前添加此视图。重量= 1的视图将伸展并填充空间

    <View
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:layout_weight="1" />

完整的示例代码在此处给出。它有4个组件。两个箭头将在左侧和左侧。文字和旋转器将在中间。

    <ImageButton
        android:id="@+id/btnGenesis"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|center_vertical"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="2dp"
        android:background="@null"
        android:gravity="left"
        android:src="@drawable/prev" />

    <View
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/lblVerseHeading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:textSize="25sp" />

    <Spinner
        android:id="@+id/spinnerVerses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:gravity="center"
        android:textSize="25sp" />

    <View
        android:layout_width="0dp"
        android:layout_height="20dp"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/btnExodus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|center_vertical"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="2dp"
        android:background="@null"
        android:gravity="right"
        android:src="@drawable/next" />
</LinearLayout>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top