在sdk/extras/android/support/percent目录下,有个jar包,仔细发现,这个jar包是支持Percent百分比布局的,所以特别学习了一下,虽然这个包出现了好久,这才注意到。好了进入正题,怎么写个Demo来使用这个jar呢?
1.将工程导入Gradle工程:
从改percent所在的目录可以看到,该工程是eclipse目录下的,所以需要转换一下,转换成gradle工程,然后导入,建议将该目录复制到一个临时的地方,自己修改成Gradle工程module后再进行导入。
注意:虽然该目录下,只有一个jar和attrs文件和AndroidManifest.xml文件,但是如果仅引用jar包,会报错的:提示找不到R.styleable 。XXX文件
或者可以直接下载aar:http://download.csdn.net/detail/zouchengxufei/9560927
之后直接导入即可!
2.代码示例:
主工程引用了上面的aar之后,就可以引用该包下的相关类资源了,该包下有以下资源:
PercentFrameLayout
PercentFrameLayout.LayoutParams
PercentLayoutHelper
PercentLayoutHelper.PercentLayoutInfo
PercentRelativeLayout
PercentRelativeLayout.LayoutParams
可以参考Sdk/docs/reference/android/support/percent/package-summary.html文件查看详情
布局代码示例:
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout
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:id="@+id/activity_percent"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.testapplication.PercentActivity">
<ImageView android:src="@drawable/a8g"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%"
/>
<View android:background="#333333"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_widthPercent="25%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="75%"
app:layout_heightPercent="25%"
/>
</android.support.percent.PercentFrameLayout>