Android布局:并排放置两个按钮

时间:2017-04-24 16:17:25

标签: android

我的应用程序设计简单:(我的第一个......;)) enter image description here

所以,你可以看到它只是一个背景和两个按钮。 (其中还有2个按钮,目前它们并不重要)

所以,我不知道如何将它们放在一起,他们所做的就是彼此重叠。我试过重量,但那没用。

这是我的axml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:minWidth="25px"
    android:minHeight="25px"
    android:background="@drawable/background">
    <Button
        android:background="@drawable/startthehatching_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/StartButton" />
    <Button
        android:background="@drawable/stopthehatching_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:id="@+id/StopButton" />
    <CheckBox
        android:text="Pro Mode On"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/CheckBoxProMode" />
    <Button
        android:text="Help!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/HelpButton" />
</LinearLayout>

我能得到一些帮助吗?它可能非常简单,但我是新手:)

谢谢!

3 个答案:

答案 0 :(得分:1)

1。创建RelativeLayout作为root布局。由于您的设计包含bottom-center对齐的CheckboxButton,因此最好将RelativeLayout用作container/root布局。

2。添加直接子级LinearLayout,此布局包含2个子RelativeLayout,两者的宽度相等。

3。STARTSTOP按钮放在相应的RelativeLayout上。

3。attribute android:layout_centerVertical="true"添加到按钮STARTSTOP以将verticallyRelativeLayout内的two对齐

4. color个不同的背景two添加到RelativeLayoutimage,而不是使用背景root {{1}布局。

仅供参考,因为我在两部分中使用了两个RelativeLayout's,所以最好使用背景color而不是image

试试这个:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="2">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#16A25B"
            android:layout_weight="1"
            android:padding="20dp">

            <Button
                android:id="@+id/StartButton"
                android:layout_width="180dp"
                android:layout_height="180dp"
                android:layout_centerVertical="true"
                android:background="@drawable/startthehatching_button" />

        </RelativeLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#16909D"
            android:layout_weight="1"
            android:padding="20dp">

            <Button
                android:id="@+id/StopButton"
                android:layout_width="180dp"
                android:layout_height="180dp"
                android:layout_centerVertical="true"
                android:background="@drawable/stopthehatching_button" />

        </RelativeLayout>
    </LinearLayout>

    <Button
        android:id="@+id/HelpButton"
        android:text="Help!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="20dp" />

    <CheckBox
        android:id="@+id/CheckBoxProMode"
        android:text="Pro Mode On"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_above="@id/HelpButton"
        android:layout_marginBottom="8dp"
        android:textColor="@android:color/white" />
</RelativeLayout>

仅供参考,您应该将height-weight Button dimens.xml定义为所有resolutions的不同<form method='post' action = 'api url'> <ul class="dropdown-menu" aria-labelledby="dropdownMenu1"> {% include 'RecordEvent/includes/EntryDropdown.html' %} </ul> </form> ,以便在所有不同设备上显示完全相同。

<强>输出:

enter image description here

希望这会有所帮助〜

答案 1 :(得分:0)

将方向更改为水平,并使其为0dp

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="center"
    android:minHeight="25px"
    android:minWidth="25px"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="2">

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:id="@+id/StartButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/startthehatching_button" />
        </FrameLayout>

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <Button
                android:id="@+id/StopButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"

                android:background="@drawable/stopthehatching_button" />
        </FrameLayout>
    </LinearLayout>
</LinearLayout>

答案 2 :(得分:0)

 this code help you!!!

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/LinearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical"
            android:minWidth="25px"
            android:minHeight="25px"
            android:background="@drawable/background">
        <LinearLayout 
         android:layout_width="match_parent"
            android:layout_height="match_parent"
           android:weightSum="2"
            android:orientation="horizontal">

            <Button
                android:background="@drawable/startthehatching_button"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/StartButton" />
            <Button
                android:background="@drawable/stopthehatching_button"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/StopButton" />
           </LinearLayout>
           <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2"
            android:orientation="horizontal">
             <CheckBox
             android:text="Pro Mode On"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:id="@+id/CheckBoxProMode" />
            <Button
             android:text="Help!"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="1"
             android:id="@+id/HelpButton" />

            </LinearLayout>
        </LinearLayout>
相关问题