Android中百分比布局


百分比布局的出現主要是因為LinearLayout中可以通過android:layout_weight="1"這種方法來支持按比例指定控件大小

但是FrameLayout和RelativeLayout中並沒有這種實現比例分配的功能,因此引入了PercentFrameLayout和PercentRelativeLayout這兩個全新的布局

接下來是PercentFrameLayout的具體應用,需要導入support庫

在app的build.gradle中添加如下的包

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:percent:25.3.1'
    testCompile 'junit:junit:4.12'
}

 


接下來對布局的安排代碼為:
<?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:id="@+id/activity_main"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.itheima.album.uilayouttest.MainActivity">
    <android.support.percent.PercentFrameLayout
        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"
        >
        <Button
            android:id="@+id/button1"
            android:text="Button 1"
            android:layout_gravity="left|top"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            />
        <Button
            android:id="@+id/button2"
            android:text="Button 2"
            android:layout_gravity="right|top"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            />
        <Button
            android:id="@+id/button3"
            android:text="Button 3"
            android:layout_gravity="left|bottom"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            />
        <Button
            android:id="@+id/button4"
            android:text="Button 4"
            android:layout_gravity="right|bottom"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            />
    </android.support.percent.PercentFrameLayout>
</FrameLayout>

 


程序運行截圖如下:

 PercentRelativeLayout實現上圖所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_main"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.itheima.album.uilayouttest.MainActivity">
    <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"
        >
        <Button
            android:id="@+id/button1"
            android:text="Button1"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            />
        <Button
            android:id="@+id/button2"
            android:text="Button2"
            android:layout_toRightOf="@id/button1"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            />
        <Button
            android:id="@+id/button3"
            android:text="Button3"
            android:layout_below="@id/button1"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            />
        <Button
            android:id="@+id/button4"
            android:text="Button4"
            android:layout_below="@id/button1"
            android:layout_toRightOf="@id/button3"
            app:layout_widthPercent="50%"
            app:layout_heightPercent="50%"
            />
    </android.support.percent.PercentRelativeLayout>
</RelativeLayout>

 


執行如下圖所示:

 

 
        
 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM