Padding與繪製地區--android:clipToPadding和android:clipChildren

來源:互聯網
上載者:User

標籤:android   布局   padding   填充   viewpager   

設計一個介面,整個介面包含一個listview,但是listview不是全部充滿螢幕,如下:

可能會這樣設計布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#2262"    android:orientation="vertical" >    <ListView        android:id="@+id/listview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_marginLeft="16dp"        android:layout_marginRight="16dp"        android:paddingTop="16dp" >    </ListView></LinearLayout>

但是這裡有個問題:滾動時頂部不能填充

這時候就需要
android:clipToPaddingandroid:clipChildren
官方文檔:

clipToPadding:控制項的繪製地區是否在padding裡面, 值為true時padding那麼繪製的地區就不包括padding地區;
定義一個孩子是否僅限於畫裡面的界限。
clipChildren:當ViewGroup的Padding不為0時,定義ViewGroup是否裁剪子孩子的填充。

這兩個屬性預設是true的,所以在設定了padding情況下,預設滾動是在 padding內部的,要達到上面的效果主要把這兩個屬性設定了false那麼這樣子控制項就能畫到padding的地區了。

修改後的代碼

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#2262"    android:orientation="vertical" >    <ListView        android:id="@+id/listview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_marginLeft="16dp"        android:layout_marginRight="16dp"        android:clipToPadding="false"        android:paddingTop="16dp" >    </ListView></LinearLayout>

再看效果

Padding與繪製地區--android:clipToPadding和android:clipChildren

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.