android5.0以上新加了宽高可按百分比配置的布局,需要导入资源库percent包,以android studio为标准:
1.在build.gradle里导入库:
dependencies {
compile 'com.android.support:percent:23.2.0'
}
注意:我第一次加这段代码的时候,这段下面画了红线说明 有错误,只要点击左边红色的小灯泡,再选第一条就可以解决,
2.现在可以用了 这个库有三个主要的 布局分别是:PercentFrameLayout,PercentRelativeLayout,PercentLinearLayout,分别对应我们常用的三种布局:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rl"
>
<com.touchdemo.view.MTextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff000"
android:gravity="center"
app:layout_heightPercent="20%"
app:layout_widthPercent="30%"
/>
</android.support.percent.PercentRelativeLayout>
以PercentRelativrLayout为例,只有包裹在其中控件才能使用百分比的属性,app:layout_heightPercent=""用百分比表示view高,
app:layout_heightPercent=""用百分比表示view宽,
还有一些其他 可以用的属性比如:
layout_marginPercent=""用百分比表示View之间的距离
layout_marginTopPercent=""用百分比表示View顶部的距离
······