日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android百分比布局初探

發(fā)布時間:2025/4/16 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android百分比布局初探 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。 https://blog.csdn.net/u010046908/article/details/48802909

標題:Android百分比布局初探

依賴庫:——com.android.support:percent

實現(xiàn)原理:

在這個包里面有兩個新的容器類 1PercentRelativeLayout
2PercentFrameLayout

在此看來,這兩個類很顯然是繼承自 FrameLayoutRelativeLayout兩個容器類。

新的容器有了一些設(shè)置百分比的屬性,下面我們來了解一下:
  • layout_widthPercent
設(shè)置控件寬度為父容器的寬的百分比
  • layout_heightPercent
設(shè)置控件高度為父容器的高的百分比
  • layout_marginPercent
  • layout_marginLeftPercent
設(shè)置控件與左邊控件的距離為父容器的寬度的百分比
  • layout_marginTopPercent
設(shè)置控件與上方控件的距離為父容器的高度的百分比
  • layout_marginRightPercent
設(shè)置控件與右邊控件的距離為父容器的寬度的百分比
  • layout_marginBottomPercent
設(shè)置控件與下方控件的距離為父容器的高度的百分比
  • layout_marginStartPercent
與上面的說明類似
  • layout_marginEndPercent
與上面的說明類似

從命名的方式我們可以知道,原來用某些具體單位(如dp)的設(shè)置現(xià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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。