【问题标题】:Android make button 1/3 of screen width and height in relative layoutAndroid在相对布局中将按钮设置为屏幕宽度和高度的1/3
【发布时间】:2016-05-18 22:10:27
【问题描述】:

我有一些 XML。在此 XML 中,相对布局中有一个按钮。相对布局占据整个屏幕。我希望按钮具有屏幕宽度和高度的 1/3。我怎么能这样做?

这是 XML:

    <?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:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    tools:context="com.vroy.trapper.MainMenuActivity"
    android:layout_weight="2">

    <Button
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:text="@string/menu_button_text"
        android:background="@drawable/round_button"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        />
</RelativeLayout>

我查看了this 问题,但即使我为相对布局本身分配了权重,我也无法为按钮分配权重。

【问题讨论】:

    标签: android xml android-button android-relativelayout


    【解决方案1】:

    您可以在 java 代码中轻松做到这一点。

    在 oncreate 中编写此代码。

        Button btn = (Button) findViewById(R.id.button);
        int width = getResources().getDisplayMetrics().widthPixels/3;
        int height = getResources().getDisplayMetrics().heightPixels/3;
        btn.setLayoutParams(new RelativeLayout.LayoutParams(width, heigth));
    

    【讨论】:

      【解决方案2】:

      百分比支持库是您需要的。

      这个库非常易于使用,因为它与我们熟悉的 RelativeLayout 和 FrameLayout 相同,只是增加了一些功能。

      首先,由于百分比支持库随 Android 支持库 23 一起提供,因此请确保您已将 SDK 管理器中的 Android 支持库更新到最新版本。然后在 build.gradle 文件中添加如下依赖项:

      编译'com.android.support:percent:23.0.0'

      现在回到你的问题,你可以做这样的事情来实现你的输出。希望对你有帮助。

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      
      <Button
          android:id="@+id/button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerHorizontal="true"
          android:layout_centerVertical="true"
          android:background="@drawable/bird"
          android:text="Your text"
          app:layout_heightPercent="33%"
          app:layout_widthPercent="33%" />
      </android.support.percent.PercentRelativeLayout>
      

      【讨论】:

      【解决方案3】:

      (编辑考虑宽度) 将按钮包裹在线性布局周围。

      <?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="0dp"
          android:paddingLeft="0dp"
          android:paddingRight="0dp"
          android:paddingTop="0dp">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal"
              android:gravity="center"
              android:weightSum="3" >
              <LinearLayout
                  android:layout_width="0dp"
                  android:layout_weight="1"
                  android:layout_height="match_parent"
                  android:orientation="vertical"
                  android:gravity="center"
                  android:weightSum="3">
      
                  <Button
                      android:id="@+id/button"
                      android:layout_width="match_parent"
                      android:layout_height="0dp"
                      android:layout_centerHorizontal="true"
                      android:layout_centerVertical="true"
                      android:layout_weight="1"
                      android:text="@string/menu_button_text"
                      android:background="@drawable/round_button"
                      />
              </LinearLayout>
          </LinearLayout>
      </RelativeLayout>
      

      如果您希望按钮位于右侧/左侧等位置,请更改 android:gravity。这样,您仍然可以将相对布局用于它周围的其他内容。

      【讨论】:

      • 您的代码版本似乎使按钮占据了屏幕高度的 1/3。但不是屏幕宽度的 1/3。也许这是因为您将宽度设置为"match_parent"
      • 我通过添加额外的 LinearLayout 进行了编辑以考虑宽度。不是最好的做事方式,因为它是嵌套布局。
      【解决方案4】:

      使用 LinearLayout 而不是 RelativeLayout 来实现您的目标。

        <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal">
      
          <View
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              />
          <LinearLayout
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:orientation="vertical"> 
              <View
                   android:layout_width="wrap_content"
                   android:layout_height="0dp"
                   android:layout_weight="1"
                   />
              <Button
                   android:layout_width="match_parent"
                   android:layout_height="0dp"
                   android:layout_weight="1"
                   android:text="@string/menu_button_text"
                   android:background="@drawable/round_button"
                   android:id="@+id/button"/>
                  <View
                   android:layout_width="wrap_content"
                   android:layout_height="0dp"
                   android:layout_weight="1"
                   />
          </LinearLayout>   
         <View
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              />
      
         </LinearLayout>
      

      希望对你有帮助!!

      【讨论】:

      • 问题是我的整体布局需要一个相对布局。
      • 我理解但不能在 RelativeLayout 中使用权重,此元素用于 LinearLayout 内的小部件 :( ...但您可以使用 RelativeLayout 作为父布局,并在其中放置我发布的带有 android 的 LinearLayout :layout_width="match_parent" 和 android:layout_height="match_parent"。这是将 RelativeLayout 与 LinearLayout 结合使用的技巧之一
      猜你喜欢
      • 2016-10-14
      • 2014-02-09
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      相关资源
      最近更新 更多