首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android布局右对齐

Android布局右对齐
EN

Stack Overflow用户
提问于 2010-11-30 00:05:02
回答 4查看 186K关注 0票数 71

我准备好了以下布局

<?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个图像按钮。第一个2,我希望彼此右旁左对齐。第三个,我想要对齐到右边。

按原样,前两个按钮是我想要的位置,但第三个按钮顽固地保持左对齐。我怎么才能让它正确对齐。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-11-30 00:16:31

布局极其低效和臃肿。你不需要那么多的LinearLayout,事实上你根本不需要任何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>
票数 147
EN

Stack Overflow用户

发布于 2010-11-30 00:13:58

您只需使用一个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参数。我只是展示了你必须设置的基本参数。你可以完成剩下的部分。

票数 21
EN

Stack Overflow用户

发布于 2013-11-19 19:17:39

如果你想使用LinearLayout,你可以用Space元素与layout_weight对齐。

例如,以下布局将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>

如果将layout_weight设置为textView2,则在不使用Space的情况下也可以达到相同的效果。只是我更喜欢分离的东西,再加上为了演示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 )。有关其他布局性能提示,请参阅Android Layout Tricks系列。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4305564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档