【问题标题】:android:how to add two button same row in framelayout? [closed]android:如何在framelayout中添加两个按钮相同的行? [关闭]
【发布时间】:2014-12-24 11:47:30
【问题描述】:

我正在尝试将两个按钮放在框架布局中。我可以使用alight left 属性使用RelativeLayout 来做到这一点,但是使用Framelayout 我没有找到任何这样的属性。如何对齐上方同一行的拖车按钮?

【问题讨论】:

    标签: android android-framelayout


    【解决方案1】:

    尝试使用 LinearLayout 来封装按钮,如下所示:

    <FrameLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity"
        android:id="@+id/main">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button1" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button2" />
        </LinearLayout>
    
    </FrameLayout>
    

    【讨论】:

      【解决方案2】:

      你可以使用 layout_gravity 属性:

      <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >
      
              <Button
                  android:layout_gravity="left"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="button 1"
                  android:id="@+id/button_1" />
      
              <Button
                  android:layout_gravity="right"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="button 2"
                  android:id="@+id/button_2" />
      
      </FrameLayout>
      

      Gravity 的值可以组合为:left|topleft|center_verticalleft|bottom 等。

      您还可以为第二个按钮使用左边距值,例如:

              <Button
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="200dp"
                  android:text="button 2"
                  android:id="@+id/button_2" />
      

      ...或者,为什么不,使用重力和边距,将按钮放置在距右边距多个 dp 的位置

              <Button
                  android:layout_gravity="right"
                  android:layout_marginRight="200dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="button 2"
                  android:id="@+id/button_2" />
      

      【讨论】:

        猜你喜欢
        • 2021-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-09
        • 1970-01-01
        相关资源
        最近更新 更多