汕头模版网站建设正规手游代理平台有哪些
一,百分比布局库android-percent-support介绍
百分比布局库中提供了两种布局可以设置百分比:PercentRelativeLayout、PercentFrameLayout。
这两个百分比布局都有以下九个布局属性,值都是用百分比来表示宽度、高度、margin值,使用时候需要父布局为百分比布局,child控件才可以使用这九个布局属性:
app:layout_heightPercent
app:layout_widthPercent
app:layout_marginPercent
app:layout_marginTopPercent
app:layout_marginBottomPercent
app:layout_marginLeftPercent
app:layout_marginRightPercent
app:layout_marginStartPercent
app:layout_marginEndPercent
可以看到支持宽高,以及margin。
也就是说,大家只要在开发过程中使用PercentRelativeLayout、PercentFrameLayout替换FrameLayout、RelativeLayout即可。
二,百分比布局使用
关于使用,其实及其简单,并且github上也有例子,android-percent-support-lib-sample。我们就简单过一下:
首先记得在build.gradle添加:
compile 'com.android.support:percent:22.2.0'
1
(一) PercentFrameLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayoutxmlns: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"><TextViewandroid:layout_width="0dp"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%"/><TextViewandroid:layout_width="0dp"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%"/><TextViewandroid:layout_width="0dp"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%"/></android.support.percent.PercentFrameLayout>
3个TextView,很简单,直接看效果图:
(二) PercentRelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayoutxmlns: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:clickable="true"><TextViewandroid:id="@+id/row_one_item_one"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%"/><TextViewandroid:id="@+id/row_one_item_two"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%"/><ImageViewandroid:id="@+id/row_two_item_one"android:layout_width="match_parent"android:layout_height="0dp"android:src="@drawable/tangyan"android:scaleType="centerCrop"android:layout_below="@+id/row_one_item_one"android:background="#d89695"app:layout_heightPercent="70%"/><TextViewandroid:layout_width="0dp"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%"/></android.support.percent.PercentRelativeLayout>
(三) 使用注意
其实PercentRelativeLayout 和PercentFrameLayout就是多了九个布局属性的RelativeLayout 和FrameLayout,用法完全和这两个布局一样,不过只有父布局是百分比布局(PercentRelativeLayout和PercentFrameLayout )的时候,child才能使用百分比布局属性进行布局,否则无效,如:
<android.support.percent.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">
<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/button"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:text="width=50%"app:layout_widthPercent="50%" /></LinearLayout>
</android.support.percent.PercentRelativeLayout>
这样设置Button的宽度并不会是父布局宽度的一半,因为child用百分比只对父布局是百分比布局才有效。三、实现PercentLinearlayout
(一) PercentLinearLayout
package com.juliengenoud.percentsamples;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 zhy on 15/6/30.*/
public class PercentLinearLayout extends LinearLayout
{private PercentLayoutHelper mPercentLayoutHelper;public PercentLinearLayout(Context context, AttributeSet attrs){super(context, attrs);mPercentLayoutHelper = new PercentLayoutHelper(this);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){mPercentLayoutHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);super.onMeasure(widthMeasureSpec, heightMeasureSpec);if (mPercentLayoutHelper.handleMeasuredStateTooSmall()){super.onMeasure(widthMeasureSpec, heightMeasureSpec);}}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b){super.onLayout(changed, l, t, r, b);mPercentLayoutHelper.restoreOriginalParams();}@Overridepublic LayoutParams generateLayoutParams(AttributeSet attrs){return new LayoutParams(getContext(), attrs);}public static class LayoutParams extends LinearLayout.LayoutParamsimplements PercentLayoutHelper.PercentLayoutParams{private PercentLayoutHelper.PercentLayoutInfo mPercentLayoutInfo;public LayoutParams(Context c, AttributeSet attrs){super(c, attrs);mPercentLayoutInfo = PercentLayoutHelper.getPercentLayoutInfo(c, attrs);}@Overridepublic PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo(){return mPercentLayoutInfo;}@Overrideprotected 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"?><com.juliengenoud.percentsamples.PercentLinearLayoutxmlns: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:orientation="vertical"><TextViewandroid:layout_width="0dp"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%"/><TextViewandroid:layout_width="0dp"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%"/><TextViewandroid:layout_width="0dp"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%"/><TextViewandroid:layout_width="0dp"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%"/><TextViewandroid:layout_width="match_parent"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%"/></com.juliengenoud.percentsamples.PercentLinearLayout>
我们纵向排列的几个TextView,分别设置宽/高都为百分比,且之间的间隔为5%p。
四,固定比例属性 layout_aspectRatio
注意:layout_aspectRatio属性需要在23.3.0版本以上才能使用
详细解释请见:http://blog.csdn.net/zcn596785154/article/details/76855499
PercentLayout是百分比控件, 使用百分比属性创建控件. 除了常用的百分比, 也可以使用固定比例进行布局(aspect ratio)
PercentLayout布局, 提供宽度和比例, 自动适配高度.
<android.support.percent.PercentRelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@+id/main_text"><ImageViewandroid:layout_width="0dp"android:layout_height="0dp"android:background="#FF1493"app:layout_aspectRatio="@fraction/header_aspectRatio"app:layout_widthPercent="50%"/></android.support.percent.PercentRelativeLayout>
比例的资源文件, fractions.xml.
<resources><item name="header_aspectRatio" type="fraction">178%</item>
</resources>
178%的含义是16:9. //宽:高
文章参考:
Android 百分比布局库(percent-support-lib) 解析与扩展
PercentLayout的固定比例
Android百分比布局支持库(android-percent-support)