博客:http://blog.csdn.net/muyang_ren
一、修改MyApplication\app目录下的build.gradle
添加 compile 'com.android.support:percent:23.1.1'
<pre name="code" class="html">dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:percent:23.1.1'
}
版本号可能会不一样
二、XML布局修改
1、RelativeLayout 修改为 android.support.percent.PercentRelativeLayout
2、添加 xmlns:app="http://schemas.android.com/apk/res-auto" 属性
3、百分比属性 app:layout_heightPercent="7%"
- layout_widthPercent
设置控件宽度为父容器的宽的百分比
- layout_heightPercent
设置控件高度为父容器的高的百分比
- layout_marginPercent
- layout_marginLeftPercent
设置控件与左边控件的距离为父容器的宽度的百分比
- layout_marginTopPercent
设置控件与上方控件的距离为父容器的高度的百分比
- layout_marginRightPercent
设置控件与右边控件的距离为父容器的宽度的百分比
- layout_marginBottomPercent
设置控件与下方控件的距离为父容器的高度的百分比
- layout_marginStartPercent
与上面的说明类似
- layout_marginEndPercent
与上面的说明类似
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lianghuiyong.myapplication.MainActivity">
<RelativeLayout
android:id="@+id/header_layout"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
app:layout_heightPercent="7%"
android:background="@color/MyAPP_header_color">
<ImageButton
android:id="@+id/header_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/header_setting"
android:layout_marginLeft="18dp"
android:layout_centerVertical="true"
android:contentDescription="imagebutton" />
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lianghuiyong.myapplication.MainActivity">
<RelativeLayout
android:id="@+id/header_layout"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
app:layout_heightPercent="7%"
android:background="@color/MyAPP_header_color">
<ImageButton
android:id="@+id/header_setting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/header_setting"
android:layout_marginLeft="18dp"
android:layout_centerVertical="true"
android:contentDescription="imagebutton" />