安卓左、中、右对齐

13

我对Android不太了解,正在尝试创建一个示例应用程序,其中3个元素(2个按钮和1个文本视图)水平放置。我需要文本在中心,两个按钮分别在视图的左侧和右侧对齐。

例如:

|<Button>    <Text>     <Button>|

希望您能帮我解决这个问题。

提前致谢。


如果您展示一下您目前的代码,那么帮助您就会更容易些。 - Phonon
3个回答

21

编辑:我相信这个代码将解决你的问题。请看下面的代码。如果有帮助,请让我知道!我知道这对我很有帮助,我会使用它的。如果有效,请接受此答案!=)

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">
    <TableRow>
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="button1" 
            android:id="@+id/button1button"></Button>
        <EditText 
            android:layout_height="wrap_content" 
            android:id="@+id/firstedittext"
            android:layout_width="wrap_content"></EditText>
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button2" 
            android:id="@+id/button2button"></Button>
    </TableRow>
</TableLayout>

编辑:添加了相对布局,看看是否适用于你,如果有问题请告诉我。你需要调整“10px”以获得所需的距离。我正在尝试更好的方法,使距离更加动态化。

你可以将按钮设置为“alignParentLeft”和“alignParentRight”,使它们与屏幕的左侧和右侧对齐。然而,我仍然无法解决两个视图之间的文本问题。

你可以在布局的xml文件中实现这一点。首先将其设为水平布局,然后添加第一个按钮,接着是文本视图,最后是第二个按钮。稍后会发布一个示例xml。

    <RelativeLayout 
        android:id="@+id/LinearLayout01"
        android:orientation="horizontal" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="button1" 
            android:layout_alignParentLeft="true"
            android:layout_marginRight="10px"
            android:id="@+id/button1button"></Button>
        <EditText 
            android:layout_height="wrap_content" 
            android:id="@+id/firstedittext"
            android:layout_toRightOf="@id/button1button"
            android:layout_marginRight="10px"
            android:layout_width="wrap_content"></EditText>
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/firstedittext"
            android:text="button2" 
            android:id="@+id/button2button"></Button>
    </RelativeLayout>

谢谢回复。我尝试了那种方式,但是它们没有对齐。 (这两个按钮需要对齐到屏幕的左侧和右侧。) 我已经将宽度设置为填满父布局。 - budsiya
我现在明白了,我修改了你的帖子,这样图片会显示得更好。它正在等待审核。当它被审核后,你应该能看到它已经出现在你的帖子中了。我从来没有成功地对齐视图中的元素,就像你说的那样。然而,当我有机会时,我会研究一下这个问题,看看是否能让它工作,因为我也在寻找一个简单的答案。 - prolink007
非常感谢您的支持。真的非常感激。我今晚回家后会尝试这两种方法。结果会告诉您。谢谢! - budsiya
@user258587:很酷,希望它能正常工作。我在工作中测试了它,对我来说完美地运行了。但是,我没有测试过如果你把手机翻过来会发生什么。祝好运。 - prolink007
为了获得高性能,请查看下面的链接,并将文本视图的宽度设置为wrap content以实现上述要求: https://dev59.com/kmgu5IYBdhLWcg3wln8O#50719521 - Manmohan Soni

19

这将解决问题。您需要的是绝对重力。这个应该适用于所有Android屏幕大小。我认为您不需要使用任何边距或填充,一旦您的子元素中有内容,它们就会使您混乱(在选择边距时,边距是相对的,即:当您的文本较少时,您希望较小的边距......这经常发生在翻译、新功能、替换等情况下。)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="button1" 
        android:layout_alignParentLeft="true"/>
    <EditText 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/firstedittext"
        android:layout_centerInParent="true"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button2" 
        android:layout_alignParentRight="true"/>
</RelativeLayout>

谢谢 Edison... 我今晚会试一下并告诉你结果。感谢你的帮助。 :) - budsiya
我到处寻找“layout_centerInParent”,但却无法找到。我以为它的命名方式与其他属性一样,如“layout_alignParentLeft”和“layout_alignParentRight”。所以,我在寻找“layout_alignParentCenter”,哈哈。 - prolink007
@prolink007 你是否使用了最新的SDK?我不确定layout_x使用了哪个SDK,但如果你只是输入android:并等待,然后输入“l”,Eclipse应该会为你过滤掉...我通常只输入“l”而不是试图记住整个内容,因为...我的记忆力太差了 T_T。 - Edison
这就是我所做的,但我输入了“layout_align”,寻找类似于“layout_alignParentCenter”的内容,哈哈。我一直以为它会和右侧和左侧一样。 - prolink007
@prolink007 哈哈,我明白了。是的,这种情况经常发生在我身上。 - Edison

0

这是我的 LinearLayout 代码。希望能对你有所帮助!

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:text="Button1" />
    </LinearLayout>

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

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:ems="10"
            android:text="Chao cac ban" >

            <requestFocus />
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:text="Button2" />
    </LinearLayout>

</LinearLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接