关于android中的百分比适配

本文介绍了一种使用百分比进行屏幕适配的方法,该方法通过引入第三方库简化了Android应用的跨屏适配工作,使开发者能够更高效地完成布局设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

大家都知道在安卓中的屏幕适配虽然不难,但是很麻烦,需要很多的切图,和大量的数据才能完成,也会增加APK文件的增大。下面来介绍一种百分比来适配的方式:

我在写程序时也是百度了很多才得到这个结论,在某篇论坛上面看到有一位大神自己封装了一套百分比的适配,非常好使用,在此专门谢谢他们。

以下是在AndroidStudio中的开发使用。

查看github上面的开源官方文档:https://github.com/hongyangAndroid/android-percent-support-extend

github上自行下载源码,可以在bintray.com/percent-support-extends 下载相关文件

首先在build文件中添加

dependencies {
    compile 'com.zhy:percent-support-extends:1.0.1'
}
然后编译一下,AS会自动下载所需要的JAR包。

包中包含一下三个类

<code class="language-xml hljs  has-numbering">com.zhy.android.percent.support.PercentLinearLayout
com.zhy.android.percent.support.PercentRelativeLayout
com.zhy.android.percent.support.PercentFrameLayout</code>


下面看具体事例:XML文件的使用


<?xml version="1.0" encoding="utf-8"?>
<com.zhy.android.percent.support.PercentLinearLayout 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="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">
    <ImageView
        android:id="@+id/more"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_widthPercent="9%w"
        app:layout_heightPercent="9%w"
        android:paddingLeft="10dip"
        android:clickable="true"
        android:layout_gravity="bottom"
        android:onClick="imageOnClick"
        android:src="@drawable/iconfontgengduo"/>
    <TextView
        android:layout_width="0dip"
        android:layout_height="1dip"
        android:layout_weight="0.5"/>
    <TextView
        android:id="@+id/titleText"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_widthPercent="25%w"
        app:layout_heightPercent="8%w"
        app:layout_textSizePercent="3.5%"
        android:gravity="center"
        android:textColor="#ecf0f1"
        android:text="渣校列表"/>
    <TextView
        android:layout_width="0dip"
        android:layout_height="1dip"
        android:layout_weight="0.5"/>
    <ImageView
        android:id="@+id/setting"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_widthPercent="8%w"
        app:layout_heightPercent="7%w"
        android:paddingRight="10dip"
        android:clickable="true"
        android:onClick="imageOnClick"
        android:src="@drawable/iconfontgengduo1"/>
</com.zhy.android.percent.support.PercentLinearLayout>



<?xml version="1.0" encoding="utf-8"?>
<com.zhy.android.percent.support.PercentLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:padding="8dip"
    android:orientation="vertical">
    <com.zhy.android.percent.support.PercentLinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_widthPercent="10%w"
        app:layout_heightPercent="12%w"
        android:gravity="center"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/iv"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/iconfontsousuo" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ecf0f1"
            android:textSize="12sp"
            android:text="搜索"/>
    </com.zhy.android.percent.support.PercentLinearLayout>
    <View
        android:layout_width="0dp"
        android:layout_height="1dp"
        app:layout_widthPercent="10%w"
        android:layout_marginTop="5dp"
        android:background="#ecf0f1"/>

</com.zhy.android.percent.support.PercentLinearLayout>

使用起来很简单,如果大家还是不太明白,请私信给我。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值