代码之家  ›  专栏  ›  技术社区  ›  Señor Reginold Francis

Android布局右对齐

  •  67
  • Señor Reginold Francis  · 技术社区  · 15 年前

    我有以下布局

    <?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个图像按钮。前两个,我要右紧挨着左对齐。第三个,我想和右边对齐。

    正如我所希望的那样,前两个按钮在我想要的地方,而第三个是stubbernly保持左对齐。我怎样才能使它正确对齐。

    5 回复  |  直到 15 年前
        1
  •  142
  •   Octavian Helm    15 年前

    布局极其低效和臃肿。你不需要那么多 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>
    
        2
  •  19
  •   Cristian    15 年前

    你只要用一个就可以做到 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参数。我只是显示了你必须设置的基本参数。剩下的你可以完成。

        3
  •  4
  •   Alexander Malakhov Rahul R Dhobi    12 年前

    如果你想用 LinearLayout ,您可以与 layout_weight 具有 Space 元素。

    例如,下列布局位置 textView textView2 一个挨着一个 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>
    

    你不需要 空间 如果你愿意 布局重量 文本视图2 . 只是我更喜欢分开的东西,还有 空间 元素。

        <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" />
    

    请注意 you should (not must though) set layout_width 显式地,因为它将根据它的重量重新计算(同样的方式,您应该在垂直元素中设置高度 LinearLayout ). 有关其他布局性能提示,请参见 Android Layout Tricks 系列。

        4
  •  3
  •   alvaroIdu    12 年前

    这是一个 相对布局 :

    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),您只需更改 相对布局 对于 线性布局 .

        5
  •  2
  •   Joseph Selvaraj    10 年前

    为了支持旧版本空间,可以用下面的视图替换。将此视图添加到最左侧组件之后和最右侧组件之前。权重为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>