FrameLayout中的Android按钮布局

时间:2013-10-11 17:30:17

标签: android android-layout android-framelayout

我有一个填充屏幕的FrameLayout和一个填充屏幕的SurfaceView。然后我有3个不同宽度的按钮。我想在屏幕的右边缘垂直排列这些按钮,均匀间隔使得顶部按钮位于屏幕顶部,中间位于中间,底部位于底部 - 此外,我希望垂直按钮中心彼此对齐。

伪布局:

<FrameLayout layout_width="match_parent" layout_height="match_parent">

    <SurfaceView layout_width="match_parent" layout_height="match_parent"></SurfaceView>

    <ToggleButton android:layout_width="45dp"></ToggleButton>
    <ToggleButton android:layout_width="67dp"></ToggleButton>
    <ToggleButton android:layout_width="100dp"></ToggleButton>

</FrameLayout>

非常感谢任何帮助,这里是我想要实现的ASCII图片,希望它有所帮助!

-------------------------------------------
|                                 (  )    |
|                                         |
|                                         |
|                                         |
|                               (      )  |
|                                         |
|                                         |
|                                         |
|                             (          )|
-------------------------------------------

3 个答案:

答案 0 :(得分:1)

试试这个

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <SurfaceView
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </SurfaceView>

        <RelativeLayout
            android:gravity="center_horizontal"
            android:layout_gravity="right"
            android:background="#f00"
            android:layout_width="100dp"
            android:layout_height="wrap_content" >

            <ToggleButton
                android:layout_width="45dp"
                  android:layout_centerHorizontal="true"
               android:layout_height="wrap_content" >
            </ToggleButton>

            <ToggleButton
                android:layout_width="67dp"
                android:layout_height="wrap_content"
               android:layout_centerHorizontal="true"
                android:layout_centerVertical="true" >
            </ToggleButton>

            <ToggleButton
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true" >
            </ToggleButton>
        </RelativeLayout>
    </FrameLayout>

</RelativeLayout>

附加快照是输出enter image description here

答案 1 :(得分:1)

试试这个

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </SurfaceView>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical" >

        <ToggleButton
            android:id="@+id/toggleFirst"
            android:layout_width="45dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" >
        </ToggleButton>

        <ToggleButton
            android:id="@+id/toggleSecond"
            android:layout_width="67dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/toggleFirst" >
        </ToggleButton>

        <ToggleButton
            android:id="@+id/toggleThird"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/toggleSecond" >
        </ToggleButton>
    </RelativeLayout>
</FrameLayout>

答案 2 :(得分:0)

您可以制作布局,然后在java代码中调用它时,只需根据屏幕宽度和按钮宽度排列按钮。将第一个按钮放在顶部,第二个按钮放在(screenheight / 2-buttonheight / 2),最后一个按钮放在(screenheight-buttonheight)。

注意:尝试根据屏幕尺寸而不是dp或sp来安排按钮,因为屏幕尺寸可能会有所不同,这会使整个事情变得混乱。

祝你好运