百分比布局介绍:http://blog.csdn.net/lmj623565791/article/details/46767825
github 地址:https://github.com/hongyangAndroid/android-percent-support-extend
用法:
1.做出1920*1080分辨率的布局文件,view和layout的宽高和间距都用px为单位;
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"> <RelativeLayout android:id="@+id/my_head_lay" android:layout_width="match_parent" android:layout_height="238px" android:background="#ffce54"> <Button android:id="@+id/my_back_btn" android:layout_width="97px" android:layout_height="97px" android:layout_alignParentLeft="true" android:layout_marginLeft="10px" android:layout_marginTop="10px" android:background="@drawable/storybook_close_bg" /> <ImageView android:id="@+id/my_logo_img" android:layout_width="321px" android:layout_height="238px" android:layout_alignParentRight="true" android:src="@drawable/my_dount_logo1" /> <Button android:id="@+id/my_picture_book_btn" android:layout_width="255px" android:layout_height="116px" android:layout_alignParentLeft="true" android:layout_marginLeft="120px" android:layout_alignParentBottom="true" android:background="@drawable/my_head_btn_select" android:text="我的绘本" android:textColor="#fff" android:textSize="20sp" android:textStyle="bold" /> <TextView android:layout_width="match_parent" android:layout_height="10px" android:layout_alignParentBottom="true" android:background="#ff6f12" /> </RelativeLayout> <GridView android:id="@+id/my_picture_book_gird_view" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/my_head_lay" android:layout_centerHorizontal="true" android:layout_marginTop="50px" android:clipToPadding="false" android:horizontalSpacing="50px" android:numColumns="3" android:paddingBottom="100px" android:paddingLeft="58px" android:paddingRight="58px" android:paddingTop="120px" android:scrollbarStyle="outsideOverlay" android:stretchMode="columnWidth" android:verticalSpacing="90px" /> <TextView android:id="@+id/my_picture_book_notag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="一个绘本都没有 ~ _ ~ !,\n请前往分级阅读观看。" android:textColor="#800000" android:textStyle="bold" android:textSize="14sp" android:gravity="center" android:visibility="gone" android:layout_centerInParent="true" /></RelativeLayout>
2.用Px2Percent.jar转换为百分比布局,Px2Percent.jar用java1.7写的。用Jar Launcher打开如下图
2.1应为我的布局为是竖屏,并且我希望按照屏幕的高来适配,这样一些像华为的手机屏幕有虚拟主页键和返回键的适配会好一些。配置如下
2.2点击 >>>转换<<<的结构如下
<?xml version="1.0" encoding="UTF-8"?>
<com.zhy.android.percent.support.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:background="#fff"
android:orientation="vertical">
<com.zhy.android.percent.support.PercentRelativeLayout
android:id="@+id/my_head_lay"
android:layout_width="match_parent"
android:layout_height="238px"
android:background="#ffce54"
app:layout_heightPercent="12.40%h">
<Button
android:id="@+id/my_back_btn"
android:layout_width="97px"
android:layout_height="97px"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10px"
android:layout_marginTop="10px"
android:background="@drawable/storybook_close_bg"
app:layout_heightPercent="40.76%h"
app:layout_marginLeftPercent="4.20%h"
app:layout_marginTopPercent="4.20%h"
app:layout_widthPercent="40.76%h" />
<ImageView
android:id="@+id/my_logo_img"
android:layout_width="321px"
android:layout_height="238px"
android:layout_alignParentRight="true"
android:src="@drawable/my_dount_logo1"
app:layout_heightPercent="100.00%h"
app:layout_widthPercent="134.87%h" />
<Button
android:id="@+id/my_picture_book_btn"
android:layout_width="255px"
android:layout_height="116px"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="120px"
android:background="@drawable/my_head_btn_select"
android:text="我的绘本"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"
app:layout_heightPercent="48.74%h"
app:layout_marginLeftPercent="50.42%h"
app:layout_widthPercent="107.14%h" />
<TextView
android:layout_width="match_parent"
android:layout_height="10px"
android:layout_alignParentBottom="true"
android:background="#ff6f12"
app:layout_heightPercent="4.20%h" />
</com.zhy.android.percent.support.PercentRelativeLayout>
<GridView
android:id="@+id/my_picture_book_gird_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/my_head_lay"
android:layout_centerHorizontal="true"
android:layout_marginTop="50px"
android:clipToPadding="false"
android:horizontalSpacing="50px"
android:numColumns="3"
android:paddingBottom="100px"
android:paddingLeft="58px"
android:paddingRight="58px"
android:paddingTop="120px"
android:scrollbarStyle="outsideOverlay"
android:stretchMode="columnWidth"
android:verticalSpacing="90px"
app:layout_marginTopPercent="2.60%h"
app:layout_paddingBottomPercent="5.21%h"
app:layout_paddingLeftPercent="3.02%h"
app:layout_paddingRightPercent="3.02%h"
app:layout_paddingTopPercent="6.25%h" />
<TextView
android:id="@+id/my_picture_book_notag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:text="一个绘本都没有 ~ _ ~ !,\n请前往分级阅读观看。"
android:textColor="#800000"
android:textSize="14sp"
android:textStyle="bold"
android:visibility="gone" />
</com.zhy.android.percent.support.PercentRelativeLayout>
这样就转换完毕,把下面的参数给转换了
com.zhy.android.percent.support.PercentLinearLayout
com.zhy.android.percent.support.PercentRelativeLayout
com.zhy.android.percent.support.PercentFrameLayout
支持的属性 :
- layout_heightPercent
- layout_widthPercent
- layout_marginBottomPercent
- layout_marginEndPercent
- layout_marginLeftPercent
- layout_marginPercent
- layout_marginRightPercent
- layout_marginStartPercent
- layout_marginTopPercent
- layout_textSizePercent
- layout_maxWidthPercent
- layout_maxHeightPercent
- layout_minWidthPercent
- layout_minHeightPercent
- layout_paddingPercent
- layout_paddingTopPercent
- layout_paddingBottomPercent
- layout_paddingLeftPercent
- layout_paddingRightPercent
注意:1.gridview gridlayout 不支持,需要用dimen或者dp;2.布局的width height用px不用wrap_content,因为工具的转换是从xml的根开始像子控件转换的。3.动态添加的view没有效果。
工具下载地址: 转换工具
================================ 分割线 ================================================
android studio 插件版 源码和插件点击这里:https://github.com/baibai2013/Px2Percent
下载这个文件 android px2dp.zip 并安装就可以了
具体用法 阅读 https://github.com/baibai2013/Px2Percent/blob/master/README.md
希望大神做出 as插件来