1. 程式人生 > >【安卓】讓子元素超出容器限制、!如何實現更靈活的容器佈局、!

【安卓】讓子元素超出容器限制、!如何實現更靈活的容器佈局、!

安卓中LinearLayout是用得最得心應手的容器,但有時候貌似不盡人意,其實安卓中的容器真的很靈活。!

1.讓子元素超出容器限制。初步想象一下,貌似子元素給定margin為負數即可超出,但事實卻是超出容器部分沒有繪製出來。

其實是可以繪製出來的,紅色容器的容器(注意是紅色控制元件的容器,不是紅色容器自己)給定android:clipChildren="false"即可,該引數預設為true,即其內元素會被其容器裁剪,注意之所以是紅色容器的容器,是因為綠色按鈕突出部分已不在紅色容器繪製範圍內了,而屬於紅色容器的容器。

     

2.下面這種佈局,按傳統模式一般最外層用LinearLayout,裡面再套RelativeLayout,以及各種噁心的排位。

其實可以來得更爽快一點,一個LinearLayout容器搞定。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:clipToPadding="false"
    android:orientation="horizontal"
    android:padding="10dp" >

    <TextView
        android:id="@+id/tvVIP"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="VIP1"
        android:textColor="#000000"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/et"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:paddingRight="20dp"
        android:text="1000" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="-20dp"
        android:text="元" />

    <Button
        android:id="@+id/btnDel"
        android:layout_width="wrap_content"
        android:layout_height="20dp"
        android:layout_marginLeft="-15dp"
        android:layout_marginTop="-10dp" />

</LinearLayout>