android百分比布局适配,Android 控件屏幕適配之百分比布局

前言

android 官方有一個百分比布局庫,可能用的小伙伴(剛開始學習的)比較少,或許沒有聽說過。

本文目的就是記錄一下自己學習的東西,這篇文章是在鴻洋大神的博客里的。我就不直接轉載,根據自己的理解進行使用。

導入依賴

鴻洋大神寫這篇博客時用的可能還是SDK 22,所以我特地去官方的看了一下,現在更新到 25,但是用官方的進行compile導入時會有錯誤,這個錯誤我也暫時不明白為什么產生,所以這里就還是使用22版本的。

在build.gradle里添加compile 'com.android.support:percent:22.2.0'

然后sync一下。

這個版本是沒有基於LinearLayout布局的,只有兩種一種是PercentFrameLayout,還有一種是PercentRelativeLayout.

具體代碼就從鴻洋大神那里copy過來,反正比較容易理解

PercentFrameLayout<?xml version="1.0" encoding="utf-8"?>

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_height="0dp"

android:layout_gravity="left|top"

android:background="#44ff0000"

android:text="width:30%,height:20%"

app:layout_heightPercent="20%"

android:gravity="center"

app:layout_widthPercent="30%"/>

android:layout_height="0dp"

android:layout_gravity="right|top"

android:gravity="center"

android:background="#4400ff00"

android:text="width:70%,height:20%"

app:layout_heightPercent="20%"

app:layout_widthPercent="70%"/>

android:layout_height="0dp"

android:layout_gravity="bottom"

android:background="#770000ff"

android:text="width:100%,height:10%"

android:gravity="center"

app:layout_heightPercent="10%"

app:layout_widthPercent="100%"/>

執行結果

縱向:

27a92f44b75a2cbac846e125c67ad983.png

橫向:

8639562e4d286c52c376d1d3ab77940d.png

PercentRelativeLayout<?xml version="1.0" encoding="utf-8"?>

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:clickable="true">

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_alignParentTop="true"

android:background="#7700ff00"

android:text="w:70%,h:20%"

android:gravity="center"

app:layout_heightPercent="20%"

app:layout_widthPercent="70%"/>

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_toRightOf="@+id/row_one_item_one"

android:background="#396190"

android:text="w:30%,h:20%"

app:layout_heightPercent="20%"

android:gravity="center"

app:layout_widthPercent="30%"/>

android:layout_width="match_parent"

android:layout_height="0dp"

android:src="@drawable/haha"

android:scaleType="centerCrop"

android:layout_below="@+id/row_one_item_one"

android:background="#d89695"

app:layout_heightPercent="70%"/>

android:layout_height="0dp"

android:layout_below="@id/row_two_item_one"

android:background="#770000ff"

android:gravity="center"

android:text="width:100%,height:10%"

app:layout_heightPercent="10%"

app:layout_widthPercent="100%"/>

執行結果

縱向:

13d3df1e58abc686763e4c34b172e297.png

橫向:

836ca230aa0e755cd16eec3a3d7c7e9f.png

接下來來實現PercentLinearLayout

具體實現邏輯並沒有怎么看懂,但是這里先貼出代碼以后再看。

依舊是鴻洋大神的代碼package com.example.rxjavatest.layout;

import android.content.Context;

import android.content.res.TypedArray;

import android.support.percent.PercentLayoutHelper;

import android.util.AttributeSet;

import android.view.ViewGroup;

import android.widget.LinearLayout;

/**

* Created by D'Russel on 2017/7/25.

*/

public class PercentLinearLayout extends LinearLayout {

private PercentLayoutHelper mPercentLayoutHelper;

public PercentLinearLayout(Context context, AttributeSet attrs)

{

super(context, attrs);

mPercentLayoutHelper = new PercentLayoutHelper(this);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

{

mPercentLayoutHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

if (mPercentLayoutHelper.handleMeasuredStateTooSmall())

{

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b)

{

super.onLayout(changed, l, t, r, b);

mPercentLayoutHelper.restoreOriginalParams();

}

@Override

public LayoutParams generateLayoutParams(AttributeSet attrs)

{

return new LayoutParams(getContext(), attrs);

}

public static class LayoutParams extends LinearLayout.LayoutParams

implements PercentLayoutHelper.PercentLayoutParams

{

private PercentLayoutHelper.PercentLayoutInfo mPercentLayoutInfo;

public LayoutParams(Context c, AttributeSet attrs)

{

super(c, attrs);

mPercentLayoutInfo = PercentLayoutHelper.getPercentLayoutInfo(c, attrs);

}

@Override

public PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo()

{

return mPercentLayoutInfo;

}

@Override

protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr)

{

PercentLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);

}

public LayoutParams(int width, int height) {

super(width, height);

}

public LayoutParams(ViewGroup.LayoutParams source) {

super(source);

}

public LayoutParams(MarginLayoutParams source) {

super(source);

}

}

}

很好,沒有看到一點注釋。留着以后慢慢看吧

具體用法:<?xml version="1.0" encoding="utf-8"?>

xmlns:app="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

android:layout_height="0dp"

android:background="#ff44aacc"

android:text="width:60%,height:5%"

android:textColor="#ffffff"

app:layout_heightPercent="5%"

app:layout_marginBottomPercent="5%"

app:layout_widthPercent="60%"/>

android:layout_height="0dp"

android:background="#ff4400cc"

android:gravity="center"

android:textColor="#ffffff"

android:text="width:70%,height:10%"

app:layout_heightPercent="10%"

app:layout_marginBottomPercent="5%"

app:layout_widthPercent="70%"/>

android:layout_height="0dp"

android:background="#ff44aacc"

android:gravity="center"

android:text="width:80%,height:15%"

android:textColor="#ffffff"

app:layout_heightPercent="15%"

app:layout_marginBottomPercent="5%"

app:layout_widthPercent="80%"/>

android:layout_height="0dp"

android:background="#ff4400cc"

android:gravity="center"

android:text="width:90%,height:5%"

android:textColor="#ffffff"

app:layout_heightPercent="20%"

app:layout_marginBottomPercent="10%"

app:layout_widthPercent="90%"/>

android:layout_height="0dp"

android:background="#ff44aacc"

android:gravity="center"

android:text="width:100%,height:25%"

android:textColor="#ffffff"

app:layout_heightPercent="25%"

app:layout_marginBottomPercent="5%"

/>

布局文件還是能看懂的,就是一個LinearLayout加上百分比。

執行結果

縱向:

324bbc694e7ee32b66c59c25cdc5f599.png

橫向:

9bfd7aa3ed50fbfd7dc3d686be1a35d2.png

好了,本文就此完結,留着以后滿滿啃吧,android一途,任重而道遠啊。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值