【问题标题】:How to center buttons on screen horizontally and vertically plus equidistant apart?如何将屏幕上的按钮水平和垂直居中加上等距?
【发布时间】:2011-02-06 13:56:21
【问题描述】:

我一直在绞尽脑汁(这里是安卓新手,所以不难做到)试图弄清楚如何做到这一点:

使用 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 的图片:

任何帮助/指导将不胜感激。

【问题讨论】:

    标签: android user-interface layout


    【解决方案1】:

    使用你们俩的建议效果很好!

    对于那些感兴趣的人,这里是 RelativeLayout 格式的最终​​ main.xml 代码,它与我试图用 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 match the given name (at ' layout_above',值为'@id/Button02')。

    通过谷歌搜索可能是什么原因,我发现由于对 Button02 的引用(通过 Button01 代码)发生在实际创建 Button02 之前,因此会产生错误。所以请注意在最终代码中 Button02 声明为 FIRST

    【讨论】:

    • 感谢这个很好的例子,这让我非常清楚相对布局和 +1 在这个例子中将 button2 放在 button1 上方
    【解决方案2】:

    使用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,句号。

    【讨论】:

    • 另外,不要使用 px(像素),使用 dip(显示独立像素)。
    • 感谢您发布完整的答案,我将其复制并粘贴到我的代码中,效果很好。
    【解决方案3】:

    这是一个使用 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>
    

    【讨论】:

      猜你喜欢
      • 2016-08-15
      • 1970-01-01
      • 2012-05-27
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 2017-05-18
      • 2010-11-20
      • 2014-07-27
      相关资源
      最近更新 更多