Android百分比布局初探
生活随笔
收集整理的這篇文章主要介紹了
Android百分比布局初探
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 https://blog.csdn.net/u010046908/article/details/48802909
標(biāo)題:Android百分比布局初探
2、PercentFrameLayout
在此看來,這兩個類很顯然是繼承自 FrameLayout和 RelativeLayout兩個容器類。
新的容器有了一些設(shè)置百分比的屬性,下面我們來了解一下:
從命名的方式我們可以知道,原來用某些具體單位(如dp)的設(shè)置現(xiàn)在都可以用百分比的方式進(jìn)行設(shè)置了,例如設(shè)置控件的寬度layout_width原來我們是這樣玩的android:layout_width="match_parent"現(xiàn)在用了百分比的屬性之后呢,可以這樣玩了app:layout_widthPercent="50%",這里的百分比是相對于父容器而言的。
官方文檔地址:https://juliengenoud.github.io/android-percent-support-lib-sample/
官網(wǎng)代碼:
1. PercentFrameLayout <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"/><ImageViewapp:layout_widthPercent="50%"app:layout_heightPercent="50%"app:layout_marginTopPercent="25%"app:layout_marginLeftPercent="25%"/></android.support.percent.PercentFrameLayout/>
2.PercentRelativeLayout
<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"><Viewandroid:id="@+id/top_left"android:layout_width="0dp"android:layout_height="0dp"android:layout_alignParentTop="true"android:background="#ff0000"app:layout_heightPercent="30%"app:layout_widthPercent="70%" /><Viewandroid:id="@+id/top_right"android:layout_width="0dp"android:layout_height="0dp"android:layout_alignParentTop="true"android:layout_toRightOf="@+id/top_left"android:background="#00ff00"app:layout_heightPercent="30%"app:layout_widthPercent="30%" /><Viewandroid:id="@+id/centre"android:layout_width="match_parent"android:layout_height="0dp"android:layout_below="@+id/top_left"android:background="#0000ff"app:layout_marginLeftPercent="10%"app:layout_marginRightPercent="20%"app:layout_marginTopPercent="10%"app:layout_marginBottomPercent="10%"app:layout_heightPercent="40%" /><Viewandroid:layout_width="match_parent"android:layout_height="0dp"android:id="@+id/bottom"android:layout_below="@+id/centre"android:background="#00f0ff"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"app:layout_heightPercent="10%"/></android.support.percent.PercentRelativeLayout>
效果:
依賴庫:——com.android.support:percent
實現(xiàn)原理:
在這個包里面有兩個新的容器類 1、PercentRelativeLayout2、PercentFrameLayout
在此看來,這兩個類很顯然是繼承自 FrameLayout和 RelativeLayout兩個容器類。
新的容器有了一些設(shè)置百分比的屬性,下面我們來了解一下:
- layout_widthPercent
- layout_heightPercent
- layout_marginPercent
- layout_marginLeftPercent
- layout_marginTopPercent
- layout_marginRightPercent
- layout_marginBottomPercent
- layout_marginStartPercent
- layout_marginEndPercent
從命名的方式我們可以知道,原來用某些具體單位(如dp)的設(shè)置現(xiàn)在都可以用百分比的方式進(jìn)行設(shè)置了,例如設(shè)置控件的寬度layout_width原來我們是這樣玩的android:layout_width="match_parent"現(xiàn)在用了百分比的屬性之后呢,可以這樣玩了app:layout_widthPercent="50%",這里的百分比是相對于父容器而言的。
官方文檔地址:https://juliengenoud.github.io/android-percent-support-lib-sample/
官網(wǎng)代碼:
1. PercentFrameLayout <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"/><ImageViewapp:layout_widthPercent="50%"app:layout_heightPercent="50%"app:layout_marginTopPercent="25%"app:layout_marginLeftPercent="25%"/></android.support.percent.PercentFrameLayout/>
2.PercentRelativeLayout
<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"><Viewandroid:id="@+id/top_left"android:layout_width="0dp"android:layout_height="0dp"android:layout_alignParentTop="true"android:background="#ff0000"app:layout_heightPercent="30%"app:layout_widthPercent="70%" /><Viewandroid:id="@+id/top_right"android:layout_width="0dp"android:layout_height="0dp"android:layout_alignParentTop="true"android:layout_toRightOf="@+id/top_left"android:background="#00ff00"app:layout_heightPercent="30%"app:layout_widthPercent="30%" /><Viewandroid:id="@+id/centre"android:layout_width="match_parent"android:layout_height="0dp"android:layout_below="@+id/top_left"android:background="#0000ff"app:layout_marginLeftPercent="10%"app:layout_marginRightPercent="20%"app:layout_marginTopPercent="10%"app:layout_marginBottomPercent="10%"app:layout_heightPercent="40%" /><Viewandroid:layout_width="match_parent"android:layout_height="0dp"android:id="@+id/bottom"android:layout_below="@+id/centre"android:background="#00f0ff"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"app:layout_heightPercent="10%"/></android.support.percent.PercentRelativeLayout>
效果:
總結(jié)
以上是生活随笔為你收集整理的Android百分比布局初探的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring context:compo
- 下一篇: Selenium for C#的入门De