如何在屏幕上水平垂直居中按钮并等距分开?

18

作为一个 Android 新手,我已经费尽心思(并不难理解)一段时间,试图找出如何实现以下目标:

输入图像描述

使用RelativeLayout或其他不同于AbsoluteLayout的布局来替换之前所创建的布局。我来自Windows编程背景,那里设备会自动调整“绝对”定位,所以GUI布局不是问题。

第一个布局在模拟器中运行得很好,但在我的Nexus One或任何屏幕大小与模拟器不同的设备上无法格式化。我预料到这一点,因为它是绝对定位的,但我还没有找到适用于不同屏幕尺寸的正确格式化解决方案。我的目标是使布局适用于不同的屏幕尺寸和纵横屏。

这是我目前正在使用的代码:[main.xml]

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
  <Button
    android:id="@+id/Button01"
    android:layout_width="188px"
    android:layout_height="100px"
    android:text="A"
    android:layout_y="50px" android:layout_x="65px" android:textSize="48sp"/>

<Button
    android:id="@+id/Button02"
    android:layout_width="188px"
    android:layout_height="100px"
    android:text="B"
    android:layout_y="175px" android:layout_x="65px" android:textSize="48sp"/>

<Button
    android:id="@+id/Button03"
    android:layout_width="188px"
    android:layout_height="100px"
    android:text="C"
    android:layout_y="300px" android:layout_x="65px" android:textSize="48sp"/>
</AbsoluteLayout>

参考其他问题,我整理了这个代码。它更接近答案,但还不够完美。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:gravity="center"
android:id="@+id/widget49"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<Button
    android:id="@+id/Button01"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:text="A"
    android:textSize="48sp"/>

<Button
    android:id="@+id/Button02"
    android:layout_width="0dip"        
    android:layout_weight="1"
    android:text="B"
    android:textSize="48sp"/>

<Button
    android:id="@+id/Button03"
    android:layout_width="0dip"      
    android:layout_weight="1"
    android:text="C"
    android:textSize="48sp"/>
</TableLayout>

这是TableLayout的图片:

输入图像描述

非常感谢任何帮助/指导。

3个回答

24

你们两位的建议都很好用,非常感谢!

如果有人感兴趣,这里是最终的main.xml代码,使用RelativeLayout格式完美地实现了我想要用AbsoluteLayout实现的效果。

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

  <Button
    android:id="@+id/Button02"
    android:layout_width="188dip"
    android:layout_height="100dip"
    android:text="B"
    android:layout_centerInParent="true"
    android:textSize="48sp" android:textStyle="bold" />

  <Button
    android:id="@+id/Button01"
    android:layout_width="188dip"
    android:layout_height="100dip"
    android:text="A"
    android:layout_centerHorizontal="true"
    android:layout_above="@id/Button02"
    android:layout_marginBottom="30dip"
    android:textSize="48sp" android:textStyle="bold" />

  <Button
    android:id="@+id/Button03"
    android:layout_width="188dip"
    android:layout_height="100dip"
    android:text="C"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/Button02"
    android:layout_marginTop="30dip"
    android:textSize="48sp" android:textStyle="bold" />

</RelativeLayout>

另外,有一个有趣的提示可能会对将来需要帮助的人有所帮助。当我根据这些建议修改了我发布的第一段代码后,尝试编译时出现了以下错误: "\res\layout\main.xml:9: error: Error: No resource found that matches the given name (at 'layout_above' with value '@id/Button02')。"

通过一些搜索发现,由于对Button02的引用(通过Button01代码)发生在Button02实际创建之前,这导致了错误。因此请注意,在最终代码中,首先声明Button02。


谢谢您提供这个好例子,这让我对相对布局有了更清晰的理解。同时,也要为在这个例子中将button2放在button1上方点赞。 - Manju

12

使用 RelativeLayout。 按钮B有android:layout_centerInParent="true"属性。按钮A有android:layout_centerHorizontal="true"android:layout_above="@id/Button02",和一些android:layout_marginBottom属性来创建所需的空白间隔。按钮C有android:layout_centerHorizontal="true"android:layout_below="@id/Button02",和一些android:layout_marginTop属性来创建所需的空白间隔。

绝对不要使用AbsoluteLayout


2
另外,不要使用 px(像素),请使用 dip(显示无关像素)。 - Mark B
谢谢您发布完整的答案,我将其复制并粘贴到我的代码中,它运行得很好。 - Ian Vink

2

这里有一个使用TableLayout的好例子。它有三个表格行,每个行的layout_weight都为1,确保每行占屏幕的1/3。中间的一行将包含您的按钮,第一行和最后一行为空。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TableRow android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>
    <TableRow android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="24sp"
                android:padding="24dp"
                android:text="Button 1"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="24sp"
                android:padding="24dp"
                android:text="Button 2"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="24sp"
                android:padding="24dp"
                android:text="Button 3"/>
        </LinearLayout>

    </TableRow>
    <TableRow android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>
</TableLayout>

编辑...

这是另一种类似的方式,可以使按钮的宽度与父元素匹配。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TableRow android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:padding="24dp"
        android:text="Button 1"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:padding="24dp"
        android:text="Button 2"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:padding="24dp"
        android:text="Button 3"/>
    <TableRow android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>
</TableLayout>

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