我可以添加一个按钮吗?

时间:2016-01-25 07:02:47

标签: android xml performance android-activity layout

我很难修复android中的布局,我试图在屏幕的左上角添加一个简单的按钮。我已经在相对布局中添加了三个按钮,它们就是我想要它们的方式。现在,我想再添加一个按钮,但每当我尝试时,它会混淆其他三个按钮:

enter image description here

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/layout"
    android:gravity="center"
    android:background="@drawable/blurbackground"    
    xmlns:ads="http://schemas.android.com/apk/res-auto" >



    <Button
        android:id="@+id/levelone"    
        android:text="Level One"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        android:onClick = "levelone"
        android:layout_width="270dp"
        android:layout_height="100dp"
        android:background="@drawable/buttonshape"      //Makes it circular
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:layout_above="@+id/leveltwo"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="64dp" />

    <Button
        android:id="@+id/leveltwo"    
        android:text="Level Two (UNLOCKED!)"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        android:onClick = "leveltwo"
        android:layout_width="270dp"
        android:layout_height="100dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="64dp" />    

    <Button
        android:id="@+id/levelthree"    
        android:text="Level Three (UNLOCKED!)"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        android:onClick = "levelthree"
        android:layout_width="270dp"
        android:layout_height="100dp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:layout_below="@+id/leveltwo"
        android:layout_alignLeft="@+id/leveltwo"
        android:layout_alignStart="@+id/leveltwo" />   

    </RelativeLayout>

请帮我添加另一个按钮而不移动其余的布局。

编辑:

我没有,也不想要工具栏! :)

9 个答案:

答案 0 :(得分:1)

在RelativeLayout中删除android:gravity="center"并添加新按钮,如下所示: enter image description here

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/layout">


<Button
    android:id="@+id/levelTop"
    android:text="Level Top Left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:shadowColor="#A8A8A8"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"/>


<Button
    android:id="@+id/levelone"
    android:text="Level One"
    android:textColor="#FFFFFF"
    android:textSize="30sp"
    android:onClick = "levelone"
    android:layout_width="270dp"
    android:layout_height="100dp"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_below="@+id/levelTop"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"/>

<Button
    android:id="@+id/leveltwo"
    android:text="Level Two (UNLOCKED!)"
    android:textColor="#FFFFFF"
    android:textSize="30sp"
    android:onClick = "leveltwo"
    android:layout_width="270dp"
    android:layout_height="100dp"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_below="@+id/levelone"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"/>

<Button
    android:id="@+id/levelthree"
    android:text="Level Three (UNLOCKED!)"
    android:textColor="#FFFFFF"
    android:textSize="30sp"
    android:onClick = "levelthree"
    android:layout_width="270dp"
    android:layout_height="100dp"
    android:shadowColor="#A8A8A8"
    android:shadowDx="0"
    android:shadowDy="0"
    android:shadowRadius="5"
    android:layout_below="@+id/leveltwo"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="20dp"/>

答案 1 :(得分:1)

here is the code exactly what you want

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

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:orientation="vertical">

        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button1"/>
    </LinearLayout>

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:weightSum="3"
        android:orientation="vertical">


        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center">

            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="button2"
                android:gravity="center"
                />

        </LinearLayout>

          <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center">

            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="button3"
                android:gravity="center"
                />

        </LinearLayout>

            <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center">

            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="button4"
                android:gravity="center"
                />

        </LinearLayout>


    </LinearLayout>


</LinearLayout>

答案 2 :(得分:0)

tool_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="@color/ColorPrimary"
    android:elevation="0dp"
    android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <Button
        android:layout_width="wrap_content"
        android:text="Buton"
        android:layout_height="wrap_content" />

    </android.support.v7.widget.Toolbar>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:background="@mipmap/background_image"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />



    </RelativeLayout>

在“活动”中包含该工具栏,如下所示

private Toolbar mToolbar;
 mToolbar = (Toolbar) findViewById(R.id.tool_bar);
this.setSupportActionBar(mToolbar);

答案 3 :(得分:0)

我将假设这不是工具栏上的按钮。

如果您在view中添加RelativeLayout,则默认情况下会将其置于左上角。因此,添加按钮后需要做的就是放置一个底部边距。

<Button
    android:id="@+id/new_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="value here" />

<Button
    android:id="@+id/levelone"    
    android:text="Level One"
    android:layout_below="@id/new_button"
    android:textColor="#FFFFFF"
    android:textSize="30sp"
    ..... />

新按钮应该是布局中的第一个视图。

顺便说一下,你的布局很差。使用RelativeLayout的想法是,views的位置相对于其他views。查看how to use relative layout android:layout_below

答案 4 :(得分:0)

As you asked, three buttons, Zeroth one at left.

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:id="@+id/layout"
        android:gravity="center"
        android:background="@drawable/blurbackground"
        xmlns:ads="http://schemas.android.com/apk/res-auto" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:orientation="vertical">

            <Button
                android:id="@+id/levelzero"
                android:text="Level zero"
                android:textColor="#FFFFFF"
                android:textSize="30sp"
                android:onClick = "levelzero"
                android:layout_width="270dp"
                android:layout_height="100dp"
                android:background="@drawable/buttonshape"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="5"
                />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:weightSum="3"
            android:orientation="vertical">


            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/levelone"
                    android:text="Level One (UNLOCKED!)"
                    android:textColor="#FFFFFF"
                    android:textSize="30sp"
                    android:onClick = "levelone"
                    android:layout_width="270dp"
                    android:layout_height="100dp"
                    android:background="@drawable/buttonshape"
                    android:shadowColor="#A8A8A8"
                    android:shadowDx="0"
                    android:shadowDy="0"
                    android:shadowRadius="5"
                    android:layout_marginBottom="150dp"
                    android:gravity="center"
                    />

            </RelativeLayout>Layout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center">

                <Button
                    android:id="@+id/leveltwo"
                    android:text="Level two (UNLOCKED!)"
                    android:textColor="#FFFFFF"
                    android:textSize="30sp"
                    android:onClick = "leveltwo"
                    android:layout_width="270dp"
                    android:layout_height="100dp"
                    android:background="@drawable/buttonshape"
                    android:shadowColor="#A8A8A8"
                    android:shadowDx="0"
                    android:shadowDy="0"
                    android:shadowRadius="5"
                    android:gravity="center"
                    android:layout_marginTop="100dp"
                    />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                >

                <Button
                    android:id="@+id/levelthree"
                    android:text="Level two (UNLOCKED!)"
                    android:textColor="#FFFFFF"
                    android:textSize="30sp"
                    android:onClick = "leveltwo"
                    android:layout_width="270dp"
                    android:layout_height="100dp"
                    android:background="@drawable/buttonshape"
                    android:shadowColor="#A8A8A8"
                    android:shadowDx="0"
                    android:shadowDy="0"
                    android:shadowRadius="5"
                    android:layout_marginTop="320dp"
                    />

            </RelativeLayout>


        </RelativeLayout>


    </RelativeLayout>

很抱歉弄乱现有代码。感谢@Devraj的线性布局代码。编辑为Relative,并添加了Margins。但这使它工作,我也测试了它。如果您有任何疑问,请告诉我。 HTH。

答案 5 :(得分:0)

请使用以下xml。需要删除gravity个中心。加上新增的按钮

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:id="@+id/layout"
        xmlns:ads="http://schemas.android.com/apk/res-auto"

        >

        <Button
            android:id="@+id/leftButton"

            android:text="Level Top"
            android:textColor="#FFFFFF"
            android:textSize="30sp"
            android:onClick = "Left top Button"
            android:layout_width="270dp"
            android:layout_height="100dp"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="5"
            android:layout_alignParentTop="true" />



        <Button
            android:id="@+id/levelone"

            android:text="Level One"
            android:textColor="#FFFFFF"
            android:textSize="30sp"
            android:onClick = "levelone"
            android:layout_width="270dp"
            android:layout_height="100dp"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
            android:layout_marginTop="130dp"
            android:layout_centerHorizontal="true" />

        <Button
            android:id="@+id/leveltwo"

            android:text="Level Two (UNLOCKED!)"
            android:textColor="#FFFFFF"
            android:textSize="30sp"
            android:onClick = "leveltwo"
            android:layout_width="270dp"
            android:layout_height="100dp"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:layout_below="@id/levelone"
            android:shadowRadius="5"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp" />


        <Button
            android:id="@+id/levelthree"

            android:text="Level Three (UNLOCKED!)"
            android:textColor="#FFFFFF"
            android:textSize="30sp"
            android:onClick = "levelthree"
            android:layout_width="270dp"
            android:layout_height="100dp"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="5"
            android:layout_below="@+id/leveltwo"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"/>


    </RelativeLayout>

希望这有帮助。

答案 6 :(得分:0)

您只需添加该按钮并使其他按钮位于其下方。请仔细阅读以下代码: <Button android:id="@+id/newBtn" android:text="Left Most Btn" android:layout_alignParentLeft="true" android:layout_alignParentTop="true"/> <Button android:id="@+id/levelone" android:layout_below="@+id/newBtn" ..../>
<Button android:id="@+id/leveltwo" android:layout_below="@+id/levelone" ..../> <Button android:id="@+id/levelthree" android:layout_below="@+id/leveltwo" ..../>

答案 7 :(得分:0)

您可以通过将Reltive重放为线性来执行良好的xml功能 行草图就像这个<LinearLayout width = "matchParent" height = "matchParent" orientation = "vertical">//// thats your main layout <LinearLayout orientation =horizontal> <Button></LinearLayout> ///1st child layout <LinearLayout orientation = vertical>// 2nd child layout <Button></Button <Button><Button> <Button></Button> </LinearLayout> </LinearLayout>

这是你现在要实现的目标的粗略歪曲

最终Vidw将是这样的

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

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:orientation="horizontal">

        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="buttonTop"/>
    </LinearLayout>

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:weightSum="3"
        android:orientation="vertical">




            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="buttonMain1"
                android:gravity="center"
                />




            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="buttonMain2"
                android:gravity="center"
                />



            <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="buttonMain3"
                android:gravity="center"

                />




    </LinearLayout>


</LinearLayout>

答案 8 :(得分:0)

我没有足够的声誉在评论部分回答你。

要放置一个额外的按钮(在您的情况下需要),您可以拖动一个新按钮并设置以下三个元素:

               android:layout_alignParentTop="true"
               android:layout_alignParentLeft="true"
               android:layout_alignParentStart="true"

如果您需要更精确,那么您可以针对引用其他/立即按钮的ID的这些元素传递值。

希望它会帮助你。

干杯!