【问题标题】:Button Alignment按钮对齐
【发布时间】:2012-06-23 07:56:55
【问题描述】:

我制作了一个相机布局,其中存在两个按钮。一个 click 按钮和一个 share 按钮。

问题是我想让这个按钮在同一个视图中对齐,但我不能。

这是我的按钮代码

<Button
    android:id="@+id/buttonClick"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click"
    android:layout_gravity="left" />

<Button
    android:id="@+id/buttonShare"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Share"
    android:layout_gravity="right" />

但我希望click 按钮与share 按钮所在的位置相同...

【问题讨论】:

  • 把它们放在Horizo​​ntal LinearLayout中
  • 当然它们都在一个水平线性布局中
  • 开发前阅读的好习惯,Luksprog 为您提供了解决方案,它解决了您的问题,但更多信息请参阅this 深入了解。

标签: android button alignment


【解决方案1】:

要更好地理解布局,请关注Layout

这里。并根据您的问题更好地使用相对布局

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" //to assign botton to bottom of layout
     >

【讨论】:

    【解决方案2】:

    您可能将它们放在另一个视图的LinearLayout 中(带有“相机演示”文本)。但是如果没有看到完整的布局文件,您可以将它们包装在另一个布局中,如下所示:

    <RelativeLayout    android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <Button
        android:id="@+id/buttonClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click"
        android:layout_alignParentLeft="true" />
    
    <Button
        android:id="@+id/buttonShare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share"
        android:layout_alignParentRight="true" />
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:
       <RelativeLayout
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_alignParentBottom="true"
                   >
      
                  <Button
                      android:id="@+id/buttonClick"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentLeft="true"
                      android:text="Click" />
      
                  <Button
                      android:id="@+id/buttonShare"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentRight="true"
                      android:text="Share" />
              </RelativeLayout>
      
      should have one more RelativeLayout as parent of all....
      

      我把它做成了compelte..

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" >
      
          <RelativeLayout
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
               >
      
              <Button
                  android:id="@+id/buttonClick"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentLeft="true"
                  android:text="Click" />
      
              <Button
                  android:id="@+id/buttonShare"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentRight="true"
                  android:text="Share" />
          </RelativeLayout>
      
      </RelativeLayout>
      

      【讨论】:

        【解决方案4】:

        从您的图像来看,您的父布局正在使用具有垂直方向的 LinearLayout。因此,您添加到其中的每个视图都会在垂直方向上一一放置。

        如果你想把两个按钮放在同一行,你必须使用RelativeLayout来包装这两个按钮。并将child放置在RelativeLayout的左右两侧

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        
            <Button
                android:id="@+id/buttonShare"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="Share" />
        
            <Button
                android:id="@+id/buttonClick"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Click" />
        
        </RelativeLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-05-10
          • 2012-09-29
          • 2015-04-06
          • 2014-10-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多