Android開發(fā)之線性布局詳解(布局權重)-創(chuàng)新互聯(lián)

布局權重

創(chuàng)新互聯(lián)公司2013年成立,先為皋蘭等服務建站,皋蘭等地企業(yè),進行企業(yè)商務咨詢服務。為皋蘭企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。

線性布局支持給個別的子視圖設定權重,通過android:layout_weight屬性。就一個視圖在屏幕上占多大的空間而言,這個屬性給其設定了一個重要的值。一個大的權重值,允許它擴大到填充父視圖中的任何剩余空間。子視圖可以指定一個權重值,然后視圖組剩余的其他的空間將會分配給其聲明權重的子視圖。默認的權重是0;

未使用權重前效果圖:

Android開發(fā)之線性布局詳解(布局權重)

倆個線性布局組件,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ScrollView
        android:id="@+id/scrollView_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
                                                                                                                                                                                                                                                                                                                                                                      
        android:orientation="vertical" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="vertical" >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/message_selected" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:padding="10dp"
                android:text="測試文字\n測試文字\n測試文字\n測試文字\n測試文字\n測試文字\n測試文字\n測試文字\n測試文字\n
                "
                android:textSize="20sp" />
        </LinearLayout>
    </ScrollView>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="嵌套Fragment" />
        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="外部Fragment" />
    </LinearLayout>
</LinearLayout>

仔細看下和想下就會發(fā)現(xiàn)這有個嚴重的問題,那就是“測試文字”行數(shù)多有,第二個LinearLayout布局被擠壓或擠出顯示區(qū),如下圖:

Android開發(fā)之線性布局詳解(布局權重)

解決方法如下,在第一個LinearLayout 中加入權重android:layout_weight="1",代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   <ScrollView
       android:id="@+id/scrollView_content"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_gravity="center_horizontal"
       android:layout_weight="1"
       android:orientation="vertical" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="vertical" >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/message_selected" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:padding="10dp"
                android:text="測試文字\n測試文字\n測試文字\n測試文字\n測試文字\n測試文字\n測試文字\n測試文字\n測試文字\n
                "
                android:textSize="20sp" />
        </LinearLayout>
    </ScrollView>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal" >
        <Button
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="嵌套Fragment" />
        <Button
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="外部Fragment" />
    </LinearLayout>
</LinearLayout>

效果如下圖

Android開發(fā)之線性布局詳解(布局權重)

這樣就算文字內容再長也不會把第二個LinearLayout  擠出顯示區(qū)或擠壓,權重布局原理:默認權重Layout_weight=0,值小的優(yōu)先布局,故第二個LinearLayout優(yōu)先布局,然后是第一個LinearLayout(設置權重值1)填充的方式布滿剩余區(qū)域,原理如圖:圖

Android開發(fā)之線性布局詳解(布局權重)

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

文章名稱:Android開發(fā)之線性布局詳解(布局權重)-創(chuàng)新互聯(lián)
文章路徑:http://muchs.cn/article8/hidip.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、網(wǎng)站排名網(wǎng)頁設計公司、動態(tài)網(wǎng)站、標簽優(yōu)化、Google

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)