如何使用百分比进行android布局?

2024-06-19

我们如何使用百分比android视图元素的值?像这样的东西

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

2018 年更新:

PercentRelativeLayout and PercentFrameLayout已弃用。考虑使用ConstraintLayout https://developer.android.com/training/constraint-layout/index.html

旧答案:

到目前为止,Android 支持库对其使用位置有限制:

  1. with RelativeLayout

    android.support.percent.PercentRelativeLayout
    
  2. with 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 http://www.androidauthority.com/using-the-android-percent-support-library-630715/:

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用百分比进行android布局? 的相关文章

随机推荐