Android ApiDemos示例解析(150):Views->Layouts->LinearLayout->10. Background Image

上一篇:/kf/201209/152591.html

利用LinearLayout 可以將幾個相關的UI空間構成一個組,將android:addStatesFromChildren設為True,這樣當組中的EditText或是Button獲取Focus時,將Layout的BackgroundImage設置成相應EditText或的Button的Drawable (按下,獲取Focus) ,此時整個Layout 的drawable 狀態和有和其子EditText或是Button的顯示效果,給用戶的感覺這個Layout中的View為一個整體。

<LinearLayout
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:addStatesFromChildren=”true”
android:gravity=”center_vertical”
android:paddingRight=”0dip”
android:background=”@android:drawable/edit_text”>

<!–
TextView label goes at the left.
–>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/linear_layout_10_to”
android:textColor=”?android:attr/textColorSecondary”
android:textAppearance=”?android:attr/textAppearanceLargeInverse”
/>

<!–
EditText goes in between.
–>
<EditText
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_weight=”1″
android:singleLine=”true”
android:background=”@null”
/> www.aiwalls.com

<!–
The button goes at the right.
–>
<ImageButton
style=”@android:style/Widget.Button.Inset”
android:src=”@android:drawable/star_big_on”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginTop=”2dip”
android:layout_marginRight=”2dip”
android:layout_marginBottom=”2dip”
android:padding=”10dip”
/>

</LinearLayout>

本例的LinearLayout 中子ViewEditText可以獲取焦點,將LinearLayout 的background設成@android:drawable/edit_text 和EditText的Drawable,並且android:addStatesFromChildren=”true”,這樣當EditText獲取焦點時, 顯示效果為整個Layout獲取焦點。這三個子View好像是一個整體:

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *