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

android:clipChildren 屬性解析——子 View 是否被裁剪

編輯:關於 Android 程式設計

android:clipChildren 屬性的預設值為 true。單從字面理解,clipChildren 的意思是「裁剪(縮短)子 View」。當我們把它的值設為 false 後,即使子控制項的高度超過父控制項,也會完整顯示而不會被壓縮。

舉例來說:父層線性佈局高度指定為 50dip,但第二個 ImageView 高度設為 70dip。此時若把 clipChildren 設為 false,這張圖片依然會正常完整地顯示,而不會只顯示一部分。反之若設為 true,超出部分就會被裁掉。

MainActivity 如下:

package cc.testclipchildren;

import android.os.Bundle;
import android.app.Activity;

/**
 * android:clipChildren 屬性的分析
 * 該屬性預設值為 android:clipChildren="true"
 * 設為 false 後,當子控制項高度高於父控制項時也會完整顯示,而不會被壓縮
 * 注意事項:在佈局的根節點設置該屬性 android:clipChildren
 */
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

main.xml 如下,關鍵是在根節點的 RelativeLayout 上設定 android:clipChildren="false":

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dip"
        android:layout_centerInParent="true"
        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="70dip"
            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" />
    </LinearLayout>
</RelativeLayout>

記住一個重點:這個屬性要設在佈局的根節點上才會生效。掌握它,就能做出子 View 溢出父容器、不被裁剪的特殊視覺效果。

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