Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android clipChildren 屬性詳解與用法示例

編輯:關於 Android 程式設計

有些布局屬性平時不太起眼,關鍵時刻卻能出奇效,clipChildren 就是其中之一。本文通過一個直觀的例子,講解它如何讓子 View 突破父容器的邊界顯示。

一、效果預覽

看到這個效果,你可能會先想到用 RelativeLayout 疊加實現,但其實借助 clipChildren 能更省事。

二、實現代碼

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:clipChildren="false" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/view_pager" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1.0" /> <LinearLayout android:layout_width="match_parent" android:layout_height="48dip" android:background="#B0C4DE" android:orientation="horizontal" > <ImageView android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1.0" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1.0" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="0dip" android:layout_height="64dip" android:layout_gravity="bottom" android:layout_weight="1.0" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1.0" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> <ImageView android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1.0" android:scaleType="fitCenter" android:src="@drawable/ic_launcher" /> </LinearLayout> </LinearLayout>

代碼說明

1. 只需在根節點設置 android:clipChildren="false" 即可,該屬性默認值為 true。

2. 可以通過 android:layout_gravity 控制超出部分如何顯示。

3. clipChildren 的含義是:是否限制子 View 在其自身範圍內繪製。設為 false 後,子 View 就能畫到父容器邊界之外。

結語

這個屬性在製作 Android 動畫、尤其是需要元素溢出容器呈現立體效果時非常有用,有需要的朋友可以參考實踐。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved