Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将屏幕上的按钮水平和垂直居中并等距放置?_Android_User Interface_Layout - Fatal编程技术网

Android 如何将屏幕上的按钮水平和垂直居中并等距放置?

Android 如何将屏幕上的按钮水平和垂直居中并等距放置?,android,user-interface,layout,Android,User Interface,Layout,我绞尽脑汁(这里是android新手,所以不难做到)想弄清楚如何做到这一点: 使用RelativeLayout或AbsoluteLayout以外的其他东西,这是创建此应用程序时使用的。我来自一个Windows编程背景,在那里,设备为您调整“绝对”位置,GUI布局不是问题 第一个布局在emulator中工作得很好,但不适用于我的NexusOne或任何其他与emulator大小不同的屏幕。我期待这一点,因为它绝对定位,但还没有找到一个解决方案,将正确的格式为不同的屏幕大小。我的目标是为不同的屏幕尺

我绞尽脑汁(这里是android新手,所以不难做到)想弄清楚如何做到这一点:

使用RelativeLayout或AbsoluteLayout以外的其他东西,这是创建此应用程序时使用的。我来自一个Windows编程背景,在那里,设备为您调整“绝对”位置,GUI布局不是问题

第一个布局在emulator中工作得很好,但不适用于我的NexusOne或任何其他与emulator大小不同的屏幕。我期待这一点,因为它绝对定位,但还没有找到一个解决方案,将正确的格式为不同的屏幕大小。我的目标是为不同的屏幕尺寸和纵向/横向布局工作

以下是我当前使用的代码:[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>

以下是表格布局的图片:


如有任何帮助/指导,将不胜感激

使用
相对值。按钮B具有android:layout\u centerInParent=“true”
。按钮A有
android:layout\u centerHorizontal=“true”
android:layout\u over=“@id/Button02”
,以及一些
android:layout\u marginBottom
来建立您想要的空白。按钮C有
android:layout\u centerHorizontal=“true”
android:layout\u lower=“@id/Button02”
,还有一些
android:layout\u marginTop
,以建立您想要的空白


不要使用
绝对布局
,句号。

使用你们两人的建议非常有效

对于那些感兴趣的人来说,这里是最后一段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:error:找不到与给定名称匹配的资源(在上面的“layout\u”处,值为“@id/Button02”)。


通过谷歌搜索可能的原因,我发现由于引用Button02(通过Button01代码)在实际创建Button02之前发生,这就产生了错误。因此请注意,在最终代码中,Button02首先声明为

,这是一个使用TableLayout的好例子。它有三个表行,每个表行的布局权重为1,确保每行占据屏幕的1/3。中间一行将包含按钮和f第一行和最后一行是空的

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


另外,不要使用像素像素,使用dip(显示独立像素)。感谢您发布完整答案,我将其复制并粘贴到我的代码中,效果非常好。感谢这个好例子,这让我非常清楚相对布局和+1在本例中将按钮2放置在按钮1上方