Android百分比适配

Android提供了新的适配方案,通过百分比分配控件的大小
具体的实现过程如下:
1.在Android Studio中使用的时候需要在gradle中导入 compile ‘com.android.support:percent:22.2.0’

2.总共提供了两种适配布局:PercentRelativeLayout、PercentFrameLayout

3.提供的属性:
layout_widthPercent, layout_heightPercent, layout_marginPercent, layout_marginLeftPercent, layout_marginTopPercent, layout_marginRightPercent, layout_marginBottomPercent, layout_marginStartPercent, layout_marginEndPercent
这里就不多解释具体的属性

4.使用
布局:

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.fiberhome.firstconclusion.MainActivity">

<com.fiberhome.firstconclusion.MyTextView
android:id="@+id/mtv"
android:layout_width="10dp"
android:layout_height="0dp"
app:layout_widthPercent="30%"
app:layout_heightPercent="20%"
app:Text="width 30%;height 20%"
app:TextColor="@android:color/holo_orange_dark"
android:layout_alignParentLeft="true"
app:layout_marginPercent="2%"
app:TextSize="10sp"
/>
<com.fiberhome.firstconclusion.MyTextView
android:id="@+id/mtv1"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_widthPercent="60%"
app:layout_heightPercent="20%"
app:Text="width 60%;height 20%"
android:layout_alignParentRight="true"
app:layout_marginPercent="2%"
app:TextColor="@android:color/holo_red_dark"
app:TextSize="15sp"
/>
<com.fiberhome.firstconclusion.MyTextView
android:id="@+id/mtv2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="@android:color/holo_blue_dark"
app:layout_heightPercent="70%"
app:Text="width 100%;height 70%"
android:layout_alignParentBottom="true"
app:layout_marginPercent="2%"
app:TextColor="@android:color/holo_blue_dark"
app:TextSize="15sp"
/>


</android.support.percent.PercentRelativeLayout>

效果图如下所示:

这里写图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值