安卓的LinearLayout如何定位三个按钮左,右和中央? [英] Android LinearLayout How to position three buttons left, right and center?

查看:775
本文介绍了安卓的LinearLayout如何定位三个按钮左,右和中央?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
   Android的左,中,右对齐

我有三个按钮,我想在水平左,中,右内的线性布局,并在父母垂直线性布局的按钮来定位。如何才能做到这一点?我没有看到任何效果,采用的android:gravity_center,右左

I have three buttons which I would like to position at the horizontal Left, Center, and Right within a linear layout and at the button of a parent vertical linear layout. How can this be done? I am not seeing any effect to applying android:gravity_center, right left.

推荐答案

只要你不想让按键之间的特定距离(可以是很难做到与不同的Andr​​oid屏幕大小),然后你想确保您填写的父母和重心添加。 href="http://developer.android.com/resources/tutorials/views/hello-linearlayout.html">演示这里的可以帮你出位。这里是一个code的例子。

As long as you don't want a specific distance between the buttons (which can be hard to do with the different Android screen sizes) then you want to make sure you fill the parent and add them with center gravity. The demo here can help you out a bit. Here is a code example.

                              

下面是生成的布局:

修改:根据您的澄清信息,最好是使用RelativeLayout的做你想要完成的任务。这表现在一些关于#1的问题。请参阅<一href="http://stackoverflow.com/questions/5623743/android-left-center-and-right-alignment/5623810#5623810">this回答。下面是从答案code。

Edit: Based on your clarifying information, it is better to use a RelativeLayout to do what you want to accomplish. This is demonstrated in a number of questions on Stackoverflow. See this answer. Here is the code from the answer.

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

这篇关于安卓的LinearLayout如何定位三个按钮左,右和中央?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆