简体   繁体   English

FrameLayout中的Android按钮布局

[英]Android button layout within FrameLayout

I have a FrameLayout that fills the screen and a SurfaceView within that, that also fills the screen. 我有一个FrameLayout填充屏幕和一个SurfaceView,它也填充屏幕。 I then have 3 buttons of different widths. 然后我有3个不同宽度的按钮。 I want to arrange these buttons vertically on the right edge of the screen, evenly spaced so the top button is at the top of the screen, middle is in the middle and the bottom one is at the bottom - also, I want the buttons vertically centre aligned with each other. 我想在屏幕的右边缘垂直排列这些按钮,均匀间隔,使顶部按钮位于屏幕顶部,中间位于中间,底部位于底部 - 此外,我希望垂直按钮中心彼此对齐。

Pseudo layout: 伪布局:

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

Any help is much appreciated and here's a ASCII picture of what I'm trying to achieve, hope it helps! 非常感谢任何帮助,这是我想要实现的ASCII图片,希望它有所帮助!

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

Try this 尝试这个

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

Attached snapshot is the output 附加快照是输出 在此输入图像描述

try this 尝试这个

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

You can make a layout and then when you call it in java code just arrange the buttons according to the width of the screen and the width of the buttons. 您可以进行布局,然后在java代码中调用它时,只需根据屏幕宽度和按钮宽度排列按钮。 put the first button on the top, second button at (screenheight/2-buttonheight/2) and the last button at (screenheight-buttonheight). 将第一个按钮放在顶部,第二个按钮放在(screenheight / 2-buttonheight / 2),最后一个按钮放在(screenheight-buttonheight)。

NOTE: Try arranging the buttons according to the size of the screen and not in terms of dp or sp because the screen sizes may vary and that would mess up the whole thing. 注意:尝试根据屏幕尺寸而不是dp或sp来安排按钮,因为屏幕尺寸可能会有所不同,这会使整个事情变得混乱。

Good Luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM