百分比布局的使用

举报
江南一点雨 发表于 2021/08/16 23:17:09 2021/08/16
【摘要】 今天有时间捣鼓了一下这个东西,和大家分享一下。 官方提供的包里,关于百分比布局有两个,如下: 就是PercentFrameLayout和PercentRelativeLayout,我们今天就来说说这两个百分比布局的使用吧。 1.添加依赖库 本文Demo使用Android Studio来完成,所以直接在Gradle文件中添加下面一行即可。 compile 'com.andr...

今天有时间捣鼓了一下这个东西,和大家分享一下。

官方提供的包里,关于百分比布局有两个,如下:

就是PercentFrameLayout和PercentRelativeLayout,我们今天就来说说这两个百分比布局的使用吧。

1.添加依赖库

本文Demo使用Android Studio来完成,所以直接在Gradle文件中添加下面一行即可。

compile 'com.android.support:percent:23.1.0'
 

2.在布局文件中使用


  
  1. <android.support.percent.PercentRelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <View
  7. android:id="@+id/top_left"
  8. android:layout_width="0dp"
  9. android:layout_height="0dp"
  10. android:layout_alignParentTop="true"
  11. android:background="#ff44aacc"
  12. app:layout_heightPercent="20%"
  13. app:layout_widthPercent="70%" />
  14. <View
  15. android:id="@+id/top_right"
  16. android:layout_width="0dp"
  17. android:layout_height="0dp"
  18. android:layout_alignParentTop="true"
  19. android:layout_toRightOf="@+id/top_left"
  20. android:background="#ffe40000"
  21. app:layout_heightPercent="20%"
  22. app:layout_widthPercent="30%" />
  23. <View
  24. android:id="@+id/bottom"
  25. android:layout_width="match_parent"
  26. android:layout_height="0dp"
  27. android:layout_below="@+id/top_left"
  28. android:background="#ff00ff22"
  29. app:layout_heightPercent="80%" />
  30. </android.support.percent.PercentRelativeLayout>
android.support.percent. PercentRelativeLayout layout_width layout_height app :layout_heightPercent= "20%" 和 app :layout_widthPercent= "70%"

上面的代码中一共有三个View,第一个View的宽为屏幕宽度的70%,高为屏幕高度的20%,第二个View位于第一个View的右边,它的宽度为屏幕宽度的30%,高为屏幕高度的20%,第三个View依次类推,那么它的效果图如下:

是不是很简单呢?在百分比布局中,除了宽高我们可以用百分数来表示之外,margin我们也可以用百分比来表示,比如下面几个属性:


  
  1. app:layout_marginStartPercent
  2. app:layout_marginEndPercent
  3. app:layout_marginTopPercent
  4. app:layout_marginBottomPercent
layout_aspectRatio


  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.percent.PercentRelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. android:background="#ff00ff22"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent">
  8. <!-- not using aspectRatio here -->
  9. <View
  10. android:id="@+id/view1"
  11. android:background="#ff44aacc"
  12. android:layout_width="100dp"
  13. android:layout_height="200dp"/>
  14. <!-- using aspectRatio here -->
  15. <View
  16. android:layout_below="@id/view1"
  17. android:background="#ffe40000"
  18. android:layout_width="100dp"
  19. android:layout_toRightOf="@id/view1"
  20. android:layout_alignParentTop="true"
  21. android:layout_height="0dp"
  22. app:layout_aspectRatio="100%"/>
  23. </android.support.percent.PercentRelativeLayout>

第一个View我们将其宽设置为100dp,高设置为200dp,第二个View我们将其宽设置为100dp,但是高设置为0dp,同时给它设置了layout_aspectRatio属性,这个属性的值为100%,表示View的宽高比为1:1,所以就看到上面的效果,如果我们将之设置为50%,表示宽高比为0.5:1,那么我们看到的效果将是这样的:

好了,最后我们再来看一个PercentFrameLayout布局的Demo:

代码如下:


  
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:id="@+id/frame_layout"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <ImageView
  8. android:id="@+id/child1"
  9. android:layout_width="0dp"
  10. android:layout_height="0dp"
  11. app:layout_heightPercent="100%"
  12. app:layout_widthPercent="100%"
  13. android:contentDescription="Image"
  14. android:src="@drawable/Image052" />
  15. <TextView
  16. android:id="@+id/child2"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="Child 2"
  20. android:textSize="24sp"
  21. android:layout_gravity="top|left" />
  22. <TextView
  23. android:textSize="24sp"
  24. android:id="@+id/child3"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="Child 3"
  28. android:layout_gravity="top|right" />
  29. </android.support.percent.PercentFrameLayout>

效果图如下:


文章来源: wangsong.blog.csdn.net,作者:_江南一点雨,版权归原作者所有,如需转载,请联系作者。

原文链接:wangsong.blog.csdn.net/article/details/50282693

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。