如何使用百分比进行android布局? [英] How to use Percentage for android layout?

查看:36
本文介绍了如何使用百分比进行android布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can we use percentage values for android view elements? something like this

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

推荐答案

2018 年更新:

PercentRelativeLayoutPercentFrameLayout 已弃用.考虑使用 ConstraintLayout

PercentRelativeLayout and PercentFrameLayout are deprecated. Consider using ConstraintLayout

旧答案:

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

So far Android Support library has limited on where to use this:

  1. 使用 RelativeLayout

android.support.percent.PercentRelativeLayout

  • FrameLayout

    android.support.percent.PercentFrameLayout
    

  • 如何使用?

    好吧,首先确保在build.grade(Module: app) 在你的安卓应用中.

    Well, first make sure to include the dependency at build.grade(Module: app) in your android app.

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

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

    Then navigate to your xml layout in this example (.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>
    

    有关详细信息,请查看此处:

    For more detail please check here:

    这篇关于如何使用百分比进行android布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆