日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android百分比布局初探

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

標題:Android百分比布局初探

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

實現原理:

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

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

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

從命名的方式我們可以知道,原來用某些具體單位(如dp)的設置現在都可以用百分比的方式進行設置了,例如設置控件的寬度layout_width原來我們是這樣玩的android:layout_width="match_parent"現在用了百分比的屬性之后呢,可以這樣玩了app:layout_widthPercent="50%",這里的百分比是相對于父容器而言的。

官方文檔地址:https://juliengenoud.github.io/android-percent-support-lib-sample/
官網代碼:
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>

效果:



總結

以上是生活随笔為你收集整理的Android百分比布局初探的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。