【问题标题】:How to do percentage height and width in Android?如何在Android中做百分比高度和宽度?
【发布时间】:2017-11-07 14:56:35
【问题描述】:

我正在尝试将按钮设置为物理设备的 60% 高度和 60% 宽度。

由于屏幕的大小取决于用户,所以使用 dips 是不可能的。

我们如何在 Android 中实现百分比高度和宽度?例如:

 <Button android:layout_width="60%" android:layout_height="60%"/>

【问题讨论】:

    标签: android layout dimension


    【解决方案1】:

    您可以使用layout_weight 属性。如果您将父布局设置为 10 权重,那么您可以在 Button 中将权重设置为 6。

    关于 Stack Overflow 的话题有很多:

    Linear Layout and weight in Android

    ...或在官方 Android 博客上:

    http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

    【讨论】:

      【解决方案2】:

      现在可以将Guidelines 定位为百分比值

      <?xml version="1.0" encoding="utf-8"?>
      <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">
      
          <Button
              android:id="@+id/button"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:text="Button"
              app:layout_constraintBottom_toTopOf="@+id/horizontalGuideline"
              app:layout_constraintEnd_toStartOf="@+id/verticalGuideline"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
          <android.support.constraint.Guideline
              android:id="@+id/verticalGuideline"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              app:layout_constraintGuide_percent="0.6" />
      
          <android.support.constraint.Guideline
              android:id="@+id/horizontalGuideline"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              app:layout_constraintGuide_percent="0.6" />
      
      </android.support.constraint.ConstraintLayout>
      

      【讨论】:

        猜你喜欢
        • 2013-08-23
        • 1970-01-01
        • 2012-02-20
        • 2012-04-05
        • 2014-02-17
        • 2013-08-20
        • 2018-08-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多