Android左,中,右对齐

bud*_*iya 13 android android-layout

我是Android的新手,并尝试做一个示例应用程序,其中3个元素(2个按钮和1个文本视图)水平放置.我需要文本位于中心,两个按钮在视图的左右对齐.

例如

|<Button>    <Text>     <Button>|
Run Code Online (Sandbox Code Playgroud)

感谢您是否可以帮助我.

提前致谢

pro*_*007 20

编辑:我相信这将解决您的问题.请参阅以下代码.如果这有帮助,请告诉我!我知道这对我有帮助,我将使用它.如果它有效,请接受这个作为答案!=)

<?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>
Run Code Online (Sandbox Code Playgroud)

编辑:添加相对布局,看看它是否适合你,让我知道它是否有效.您需要调整"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>
Run Code Online (Sandbox Code Playgroud)


Edi*_*son 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>
Run Code Online (Sandbox Code Playgroud)