如何在Android布局中使用百分比?

39

我们如何在Android视图元素中使用百分比值?就像这样

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

1
除了答案中提到的库,您还可以在LinearLayout中使用android:layout_weight来实现类似的目的:https://github.com/commonsguy/cw-omnibus/blob/master/Containers/LinearPercent/res/layout/main.xml - CommonsWare
@CommonsWare 但是我们如何在单个布局中同时设置宽度和高度的百分比? - Cyanotis
2
@Cyanotis:不,除非你使用LinearLayout容器来嵌套并通过android:layout_weight控制两个轴上的百分比。毫无疑问,新的android.support.percent类更加灵活。我只是想指出经典方法,因为有些人反对使用库。 - CommonsWare
@CommonsWare 好的。我以为还有其他方法。谢谢回复。 - Cyanotis
这是一个老问题,但没有新的答案。 - normidar
6个回答

49

更新 2018:

PercentRelativeLayoutPercentFrameLayout 已经被弃用。考虑使用ConstraintLayout

旧回答:

目前 Android Support 库仅限在以下情况下使用:

  1. RelativeLayout 一起使用

android.support.percent.PercentRelativeLayout
  • 使用 FrameLayout

    android.support.percent.PercentFrameLayout
    
  • 如何使用它?

    首先,请确保在您的Android应用程序的 build.grade (Module: app) 中包含依赖项。

    <code>dependencies {
        compile 'com.android.support:percent:23.3.0'
    }
    </code>

    然后在这个示例中导航到你的xml布局文件(.MainActivity)

    <code><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>
    </code>

    更多细节请查看此处


    13
    支持库刚刚添加了一个百分比布局。
    可以像这样完成。
     <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


    11
    您可以使用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支持在您的应用程序中添加和管理基于百分比的尺寸。

    要使用此功能,您需要将此库添加到 Gradle 依赖项列表中:

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

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


    10

    由于26.0.0版本中,PercentFrameLayoutPercentRelativeLayout两个控件已被弃用,因此您需要使用ConstraintLayout来按百分比大小设置您的TextView。

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

    以下是一个示例,演示如何使用ConstraintLayout调整TextView的大小(宽度为75%,高度为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%:

    Image-1

    Image-2


    5

    谷歌推出了一个名为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>
    

    3
    这里有几个错误。你的开头和结尾标签不同(Relative/Frame),但更重要的是,这些属性是错误的,应该是app:layout_widthPercent="75%" app:layout_heightPercent="50%" - Lewis McGeary
    已编辑。这是一个打字错误,因为我试图复制原帖的示例。 - N J

    3
    你可以使用 PercentRelativeLayout,它是 RelativeLayout 的一个子类,支持基于百分比的尺寸和边距。
    要使用它,你需要将这个库添加到你的 Gradle 依赖 中:
    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>
    

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接