【问题标题】:How to get a relative layout height to wrap around a button?如何获得相对布局高度以环绕按钮?
【发布时间】:2015-03-31 01:58:19
【问题描述】:

我正在尝试使用相对布局来环绕按钮。它看起来像这样:

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/Theme.MyCompany.Button.Container"
            android:layout_weight="10">

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_gravity="center"
                style="@style/Theme.MyCompany.Button.MyList"
                android:id="@+id/this is my button"
                android:text="@string/this_is_my_button"/>

        </RelativeLayout>

它在宽度方面包裹得很好,但高度达到了上面的 LinearLayout。

我试图通过创建一个白色的按钮主题来破解它,但它只是创建了一个更轻的视图。

如何让这个按钮出现在页面底部,无论是否使用容器?

【问题讨论】:

  • 这个传说中的“LinearLayout”在哪里?

标签: android android-layout button android-relativelayout


【解决方案1】:

如果我理解你的问题是正确的,你需要将这一行 android:layout_alignParentBottom="true" 移动到相对布局

所以是这样的:

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Theme.MyCompany.Button.Container"
        android:layout_alignParentBottom="true"
        android:layout_weight="10">

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            style="@style/Theme.MyCompany.Button.MyList"
            android:id="@+id/this is my button"
            android:text="@string/this_is_my_button"/>

    </RelativeLayout>

【讨论】:

    【解决方案2】:

    试试这个只需更新相对布局的“android:layout_height”--

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            style="@style/Theme.MyCompany.Button.Container"
            android:layout_weight="10">
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_gravity="center"
                style="@style/Theme.MyCompany.Button.MyList"
                android:id="@+id/this is my button"
                android:text="@string/this_is_my_button"/>
    
        </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      代码中的一些更改,试试这个

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >
      
          <RelativeLayout  //this will wrap your button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_weight="10" >
      
              <Button
                  android:id="@+id/this is my button"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:text="this_is_my_button" />
          </RelativeLayout>
      
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 2011-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多