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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android 第七课 4种基本布局之FrameLayout和百分比布局

發布時間:2023/12/10 55 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 第七课 4种基本布局之FrameLayout和百分比布局 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

FrameLayout(幀布局),她沒有方便的定位方式,所有的控件都會默認擺放在布局的左上角。

修改activity_main.xml中的代碼,如下:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"> <TextViewandroid:id="@+id/text_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="This is TextView"/><ImageViewandroid:id="@+id/image_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher"/> </FrameLayout>

運行程序之后,可以看到文字和圖片都是位于布局的左上角,由于ImageView是在TextView之后添加的因此圖片壓在了文字的上面。這是一種默認效果,除了這種情況之外,我們還可以使用layout_gravity屬性來指定控件在布局中的對齊方式,這和LinearLayout中的用法是相似的,修改activity_main.xml中的代碼,如下:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.example.uilayouttest.MainActivity"><TextViewandroid:id="@+id/text_view"android:layout_width="wrap_content"android:layout_height="wrap_content" android:layout_gravity="left"android:text="This is TextView"/><ImageViewandroid:id="@+id/image_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="right"android:src="@mipmap/ic_launcher"/></FrameLayout>

總的來說,FrameLayout定位方式欠缺,所以應用場景也少。


  • 百分百布局

可以發現只有LinearLayout支持使用layout_weight屬性來實現按比例指定控件大小的功能,其他兩種不支持。

百分百布局中,我們不再使用wrap_content、match_parent等方式來指定控件的大小,而是允許直接指定控件在布局中所占的百分比,這樣的話就可以輕松實現評分布局甚至是任意比例分割布局的效果了。

LinearLayout本身已經支持按比例指定控件的大小了,因此百分比布局只為了FrameLayout和RelativeLayout進行了功能擴展,提供了PercentFrameLayout和PercentRelative這兩個全新的布局。

不同于前三種,百分比布局屬于新增布局,Android 團隊將百分比布局定義在了support庫當中,我們只需要在項目的build。gradle中添加百分比布局庫的依賴。打開app/build.gradle文件,在dependencies閉包中添加了如下內容:

dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])implementation 'com.android.support:appcompat-v7:26.1.0'compile 'com.android.support:percent:24.2.1'implementation 'com.android.support.constraint:constraint-layout:1.0.2'testImplementation 'junit:junit:4.12'androidTestImplementation 'com.android.support.test:runner:1.0.1'androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' }

接下來修改activity_main.xml中的代碼,如下:


<?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:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"> <Buttonandroid:id="@+id/button1"android:text="Button 1"android:layout_gravity="left|top"app:layout_widthPercent = "%50"app:layout_heightPercent = "%50"/><Buttonandroid:id="@+id/button2"android:text="Button 2"android:layout_gravity="right|top"app:layout_widthPercent = "%50"app:layout_heightPercent = "%50"/><Buttonandroid:id="@+id/button3"android:text="Button 3"android:layout_gravity="left|bottom"app:layout_widthPercent = "%50"app:layout_heightPercent = "%50"/><Buttonandroid:id="@+id/button4"android:text="Button 4"android:layout_gravity="left|bottom"app:layout_widthPercent = "%50"app:layout_heightPercent = "%50"/></android.support.percent.PercentFrameLayout>

最外層我們使用了PercentFrameLayout,由于百分比布局并不是內置在系統SDK當中的,所有需要把完整的包路徑寫出來。然后還必須定義一個app的命名空間,這樣才能使用 百分比布局的自定義屬性。

在PercentFrameLayout中我們定義了4個按鈕,使用app:layout_widthPercent屬性將各按鈕的寬度指定為布局的50%,

使用app:layout_heightPercent屬性將各按鈕的高度指定為布局的50%,不過PercentFrameLayout還是會繼承FrameLayout的特性,即所有的控件默認擺放在布局的左上角。為了不讓布局重疊,我們還是借助了layout_gravity來分別將這4個按鈕放置在布局的左上,右上,左下,右下4個位置。

運行程序如圖:

(待續。。。)

PercenFrameLayout就介紹到這里,還有一個PercentRelativeLayout的用法也是非常相似的。它繼承了RelativeLayout等布局。






總結

以上是生活随笔為你收集整理的Android 第七课 4种基本布局之FrameLayout和百分比布局的全部內容,希望文章能夠幫你解決所遇到的問題。

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