【问题标题】:How to use Percentage for android layout?如何使用百分比进行 android 布局?
【发布时间】:2015-11-20 21:14:24
【问题描述】:

我们如何为 android 视图元素使用 percentage 值?像这样的

<TextView
        android:id="@+id/"
        android:layout_width="75%"
        android:layout_height="50%"/>

【问题讨论】:

  • 除了答案中引用的库之外,您可以在LinearLayout 中使用android:layout_weight 来实现类似的目标:github.com/commonsguy/cw-omnibus/blob/master/Containers/…
  • @CommonsWare 但是我们如何在单一布局中同时设置宽度和高度的百分比?
  • @Cyanotis:不,除了嵌套LinearLayout 容器并通过android:layout_weight 沿两个轴控制百分比。毫无疑问,新的android.support.percent 类更加灵活。我只是想指出经典的方法,因为有些人反对使用库。
  • @CommonsWare 好的。我以为还有其他方法。感谢重播。

标签: android android-view android-preferences android-styles


【解决方案1】:

2018 年更新:

PercentRelativeLayoutPercentFrameLayout 已弃用。考虑使用ConstraintLayout

旧答案:

到目前为止,Android 支持库限制了在哪里使用它:

  1. RelativeLayout

    android.support.percent.PercentRelativeLayout
    
  2. FrameLayout

    android.support.percent.PercentFrameLayout
    

如何使用它?

好吧,首先确保将依赖项包含在 build.grade(Module: app) 在你的安卓应用中。

dependencies {
    compile 'com.android.support:percent:23.3.0'
}

然后导航到本示例中的 xml 布局 (.MainActivity)

<android.support.percent.PercentRelativeLayout

  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  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"
  tools:context=".MainActivity">


  <Button
    android:id="@+id/button"
    android:text="Button"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    app:layout_widthPercent="30%"/>

  <Button    
    android:id="@+id/button2"
    android:text="Button 2"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/button"
    app:layout_widthPercent="60%"/>

  <Button
    android:id="@+id/button3"
    android:text="Button 3"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    app:layout_widthPercent="90%"/>

</android.support.percent.PercentRelativeLayout>

更多详情请查看here

【讨论】:

    【解决方案2】:

    支持库刚刚添加了百分比布局。

    这样就可以了

     <android.support.percent.PercentFrameLayout
         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">
         <ImageView
             app:layout_widthPercent="50%"
             app:layout_heightPercent="50%"
             app:layout_marginTopPercent="25%"
             app:layout_marginLeftPercent="25%"/>
     </android.support.percent.PercentFrameLayout>
    

    https://developer.android.com/reference/android/support/percent/PercentFrameLayout.html

    【讨论】:

      【解决方案3】:

      您可以使用PercentRelativeLayout, 它是 Design Support Library 的最新未记录补充,不仅可以指定元素之间的相对关系,还可以指定可用空间的总百分比。

      结构是

      <android.support.percent.PercentRelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          >
          <ImageView
              android:id="@+id/imageview"
              android:layout_gravity="center"
              app:layout_widthPercent="50%"
              app:layout_heightPercent="50%"
              android:src="@mipmap/ic_launcher"
              android:background="#cecece"/>
          <TextView
              android:id="@+id/textview1"
              android:layout_below="@id/imageview"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              app:layout_marginStartPercent="25%"
              app:layout_marginEndPercent="25%"
              android:text="PercentRelativeLayout example"
              android:background="#bebebe"/>
      
      
      </android.support.percent.PercentRelativeLayout>
      

      百分比包提供了 API 来支持在您的应用中添加和管理基于百分比的维度。

      要使用,需要将此库添加到your Gradle dependency列表中:

      dependencies {
          compile 'com.android.support:percent:22.2.0'
          // or compile 'com.android.support:percent:23.1.0'
      }
      

      出于演示目的,您可以访问 Git android-percent-support-lib-sample

      【讨论】:

        【解决方案4】:

        由于 PercentFrameLayoutPercentRelativeLayout 在 26.0.0 中被弃用,您需要使用 ConstraintLayout 使用百分比值调整 TextView 的大小。

        ConstraintLayout 是为 Android 平台构建响应式 UI 的强大工具,您可以在此处找到更多详细信息 Build a Responsive UI with ConstraintLayout

        以下是如何使用 ConstraintLayout 调整 TextView(w=75%, h=50%) 大小的示例:

        <android.support.constraint.ConstraintLayout
            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">
        
            <android.support.constraint.Guideline
                android:id="@+id/ver_guideline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constraintGuide_percent="0.75"/>
        
            <android.support.constraint.Guideline
                android:id="@+id/hor_guideline"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                app:layout_constraintGuide_percent="0.5"/>
        
            <TextView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="0dp"
                android:layout_marginEnd="0dp"
                android:layout_marginStart="0dp"
                android:layout_marginTop="0dp"
                app:layout_constraintBottom_toTopOf="@+id/hor_guideline"
                app:layout_constraintEnd_toStartOf="@+id/ver_guideline"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        
        </android.support.constraint.ConstraintLayout>
        

        不要忘记将constraint-layout 依赖添加到您的模块build.gradle 文件中

        dependencies {
            ...
            implementation 'com.android.support.constraint:constraint-layout:1.0.2'
            ...
        }
        

        或者直接在Layout Editor中编辑你的布局,如下图:

        因此,您的TextView 宽度是父宽度的 75%,高度是父高度的 50%:

        【讨论】:

          【解决方案5】:

          Google 有名为 android.support.percent

          的新 API

          例如PercentRelativeLayout

           <android.support.percent.PercentFrameLayout
                   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">
               <TextView
                   android:id="@+id/myTextView"
                   app:layout_widthPercent="75%"
                   app:layout_heightPercent="50%"/>
           </android.support.percent.PercentFrameLayout>
          

          【讨论】:

          • 这里有一些错误。你的开始和结束标签是不同的(相对/框架),但更重要的是,那些属性是错误的,应该是app:layout_widthPercent="75%" app:layout_heightPercent="50%"
          • yup 编辑说这是类型错误,因为我正在尝试复制 OPs 示例
          【解决方案6】:

          您可以使用PercentRelativeLayout,它是RelativeLayout 的子类,支持基于百分比的尺寸和边距。

          要使用,您需要将此库添加到您的Gradle dependency

          dependencies {
              compile 'com.android.support:percent:23.1.0'
          }
          

          示例结构是

          <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">
               <ImageView
                   app:layout_widthPercent="50%"
                   app:layout_heightPercent="50%"
                   app:layout_marginTopPercent="25%"
                   app:layout_marginLeftPercent="25%"/>
          </android.support.percent.PercentRelativeLayout>
          

          【讨论】:

            猜你喜欢
            • 2011-09-06
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-05
            • 1970-01-01
            • 2011-10-18
            • 2010-11-21
            相关资源
            最近更新 更多