【问题标题】:Can I add a button?我可以添加一个按钮吗?
【发布时间】:2016-05-01 10:05:34
【问题描述】:

我在修复 android 中的布局方面非常糟糕,我正在尝试在屏幕的左上角添加一个简单的按钮。我已经在相对布局中添加了三个按钮,它们正是我想要的样子。现在,我想再添加一个按钮,但每当我尝试时,它就会把其他三个按钮弄乱:

这是我的代码:

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

请帮我在不移动其余布局的情况下添加另一个按钮。

编辑:

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

【问题讨论】:

  • 添加自定义工具栏,其中添加按钮
  • 这个 xml 显示的输出是什么?
  • 左上角是指Action Bar中的图标还是没有Action Bar的Activity左上角的图标?
  • @Anirudh 抱歉,如果不清楚,我没有操作栏。我只想要它在屏幕的左上角
  • @MustansarSaeed 现在我已经完美了,三个按钮垂直。现在,我想添加另一个按钮,而不会弄乱其他三个按钮,我该怎么做?谢谢!

标签: android xml performance android-activity layout


【解决方案1】:

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

【讨论】:

  • 很好用,但他要求相对布局。
  • 干得好,它是 OP 应该使用的最佳方法,以避免弄乱布局。
  • @RuchirBaronia 是的,它可以是水平的。如果您只在其中添加一个按钮,则不会发生任何变化。如果你想添加更多元素,那么只有你才能看到效果。
  • @Anirudh,我个人觉得相对布局比Linear乱。您将花费相同的时间来修复一个相对布局和十个 5 线性布局。
  • 是的,我自己使用线性布局。当需要处理的组件数量过多时,我使用相对布局,便于指定位置。
【解决方案2】:

删除android:gravity="center"in RelativeLayout 并添加新按钮,如下所示:

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

【讨论】:

  • @RuchirBaronia :删除RelativeLayout 中的android:gravity="center" 并再次设置4 按钮的位置。试试看
  • 所以你把重力改成了android:layout_centerHorizontal="true"?
  • @NullPointerException 是的,如果你为RelativeLayout 设置android:gravity="center",像android:layout_alignParentTop="true"android:layout_alignParentLeft="true" 这样的另一个属性将不起作用。所以我删除android:gravity="center" 并设置android:layout_centerHorizontal="true"
【解决方案3】:

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

【讨论】:

  • 看他的评论,他没有操作栏,他需要在屏幕左上角。
  • 是的,如果我不清楚,很抱歉,但我没有工具栏。非常感谢您的回答,请告诉我该怎么做!
  • 有没有我可以在没有操作栏的情况下做到这一点?我不想要一个动作栏。此外,我将不断更改按钮在 java 中的可见性,所以你介意告诉我如何在没有操作栏的情况下做到这一点吗?谢谢!
  • 将操作栏 xml 设置为布局并将其包含在 xml 中并在提示中制作
【解决方案4】:

请使用以下xml。 gravity center 需要被移除。加上新按钮

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

希望这会有所帮助。

【讨论】:

  • 删除了gravity,添加了新按钮,并将其他三个按钮从layout_above更改为lyaout_below,相对于第一个按钮。
  • gravity 将把它的所有孩子放到center 因此,top left 的要求不能用这个来实现
  • 它与Level One按钮是第一个按钮的良好做法有关,因此应先添加它,然后根据此按钮调整所有其他按钮,而不是从底部添加Level Three按钮并调整其他按钮按钮
【解决方案5】:

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

如果在RelativeLayout 中添加view,则默认放置在左上角。所以添加按钮后你需要做的是放置一个底部边距。

<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 layoutandroid:layout_below等。

【讨论】:

  • @NullPointerException 查看我编辑的答案。我添加了android:layout_below="@id/new_button",这将使levelone 按钮位于新按钮下方
  • @NullPointerException 在您的leveltwo 按钮中,您还应该添加android:layout_below="@id/levelone"
  • @NullPointerException 也尝试删除相关布局中的填充。
  • 为什么要删除填充?谢谢!
  • 还有,你为什么把它放在下面而不是上面
【解决方案6】:

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

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

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

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

希望对你有所帮助。

干杯!

【讨论】:

  • @Ruchir :具体到这个问题 android:layout_alignParentTop="@+id/levelone" android:layout_alignParentLeft="true" android:layout_alignParentStart="@+id/levelone"
  • Answer @Dahn DC 反映了我在这里解释的相同逻辑。相对布局可以轻松实现这个设计。
【解决方案7】:

<?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 提供线性布局的代码。将其编辑为相对,并添加边距。但这使它起作用,我也对其进行了测试。如果您有任何疑问,请告诉我。 HTH。

【讨论】:

    【解决方案8】:

    您可以通过将相对于线性替换为良好的 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>

    这是您现在要实现的目标的粗略草图

    最终的视频会是这样的

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

    【讨论】:

    • 不是第一个孩子是垂直的,主布局是垂直的,因为它需要垂直2个分区,然后在第一个孩子中,子孩子被放置在水平位置,第二个孩子必须以堆叠方式放置,所以将它们排列在垂直方向
    • 但是您在粗略草图中的第二个子视图中有orientation =horizontal&gt;
    • 到目前为止,您有一个孩子,因为使用水平或垂直没有涟漪效应,但如果有多个孩子,则会影响布局,因此对于单个孩子,可以垂直使用或使用水平
    • 嘿,我有一个关于 imageButtons 的一般性问题。当我在src 属性中指定图像时,图像要么太小,要么太大。我是否应该始终在 background 属性中指定它?
    【解决方案9】:

    您所要做的就是添加该按钮并将其他按钮置于其下方。请通过以下代码: <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" ..../>

    【讨论】:

      猜你喜欢
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 2022-12-04
      • 2021-07-14
      • 1970-01-01
      • 2011-01-31
      • 2011-01-02
      • 2018-11-30
      相关资源
      最近更新 更多