安卓学习--百分比适配

博客: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" />



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值