Support Library -- Percent

一、概念

Percent Support Library是Google百分比布局兼容函数库,进行屏幕适配。

二、属性

使用android.support.percent.PercentRelativeLayout和android.support.percent.PercentFrameLayout分别替代掉RelativeLayout和FrameLayout。
现有以下属性可以使用:

layout_widthPercent : 宽度的百分比 ,例如:app:layout_widthPercent="25%"
layout_heightPercent : 高度的百分比
layout_marginPercent : 边距的百分比
layout_marginLeftPercent
layout_marginRightPercent
layout_marginTopPercent
layout_marginBottomPercent
layout_marginStartPercent
layout_marginEndPercent

layout_aspectRatio : 用百分比来表示View的宽高比。

三、使用

使用Percent Support Library例子

首先需要在build.gradle文件中添加依赖:

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

代码如下:

<android.support.percent.PercentRelativeLayout 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">
 
    <View
        app:layout_widthPercent="25%"
        android:layout_height="100dp"
        app:layout_marginLeftPercent="5%"
        android:background="#ff0000" />
 
</android.support.percent.PercentRelativeLayout>

不使用Percent Support Library例子

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="20">
 
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            />
 
        <View
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="5"
            android:background="#ff0000" />
 
    </LinearLayout>
 
</RelativeLayout>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容