如何中心屏幕上的按钮水平和垂直加等距间隔?间隔、按钮、水平和、屏幕上

2023-09-05 08:39:59 作者:岁就超神ㄩ

我一直在货架我的大脑(Android的新手在这里,所以并不难做到)一段时间试图找出如何做到这一点:

I've been racking my brain (android newbie here, so not hard to do) for awhile trying to figure out how to accomplish this:

使用RelativeLayout的还是东西比AbsoluteLayout其他这是什么这是与创建。我从Windows编程背景,其中设备调整绝对的定位,为您和GUI布局未来是一个不是问题的问题。

using a RelativeLayout or something other than AbsoluteLayout which is what this was created with. I'm coming from a Windows programming background where the device adjusts the 'absolute' positioning for you and GUI layout was a non-issue.

第一个布局模拟器的伟大工程,但对我的Nexus One或不同于仿真器大小的任何其他屏幕没有格式化。我预计,因为它是绝对定位的,但还没有发现,将正确地格式化为不同屏幕尺寸的解决方案。我的目标是有不同的屏幕尺寸和纵向/横向布局工作。

The first layout works great in the emulator, but doesn't format for my Nexus One or any other screen that differs from the emulator size. I expected this because it's absolutely positioned, but haven't found a solution that will format correctly for different screen sizes. My goal is to have the layout work for different screen sizes and in portrait / landscape.

下面是我目前使用的code:[main.xml中]

Here's the Code that I'm currently using: [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>

使用来自其他问题花絮在这里,我想到了这一点,它更接近,但还没有。

Using tidbits from other questions here, I came up with this, it’s closer, but not there yet.

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

Here’s a picture of the TableLayout:

任何帮助/指导将大大AP preciated。

Any help / guidance would be greatly appreciated.

推荐答案

用从你们俩非常完美的建议!非常感谢你的帮助,现在我可以学习这一点,并运用我学到的东西。

Using the recommendations from both of you worked perfectly! Thank you so much for your help and now I can study this and apply what I learned.

对于那些有兴趣,这里是最后的main.xml code在RelativeLayout的格式,匹配什么,我试图做的AbsoluteLayout ..

For those interested, here's the final main.xml code in RelativeLayout format that matched what I was trying to do with the 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>

此外,在一个有趣的注意,可以帮助别人的未来。当我修改第一code我贴这些建议,我收到在编译的时候这个错误: \水库\布局\ main.xml中:9:错误:错误:未发现的资源匹配给定名称(在'layout_above,值为@ ID / Button02)。

使用上可能是什么原因有点谷歌搜索,我发现,自从参考Button02(通过Button01 code)发生Button02实际创建之前,这所产生的错误。所以,注意在C Button02最后$ C $声明第一个。希望这可以帮助别人。

With a little Googling on what could be the cause, I discovered that since the reference to Button02 (via the Button01 code) occurred before Button02 was actually created, this generated the error. So note in the final code Button02 is declared FIRST. Hope this helps someone.

 
精彩推荐
图片推荐