1. 程式人生 > >Android View非常用屬性介紹

Android View非常用屬性介紹

android:addStatesFromChildren   (ViewGroup屬性)
當設定為true的時候,子控制元件可以獲取焦點的控制元件(比如EditText)獲取焦點的時候,呼叫
addStatesFromChildren屬性的View的背景也隨著變化。
官方說法(Sets whether this ViewGroup's drawable states also include
     * its children's drawable states.  This is used, for example, to
     * make a group appear to be focused when its child EditText or button
     * is focused.)
設定ViewGroup的圖片狀態是否跟隨(可以獲取焦點的View)字View是否獲取焦點來變化。

<LinearLayout
android:layout_marginTop="100dp"
android:addStatesFromChildren="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/bg"
android:gravity="center_horizontal"
>
    <TextView
android:layout_marginTop=
"20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="UserName" /> <EditText android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="100dp" android:background="#11ee44" /> <TextView android:layout_width="match_parent"
android:layout_height="100dp" android:text="TEST" /> </LinearLayout>
android:alwaysDrawnWithCache定義子佈局是否應用繪圖的快取記憶體
android:animateLayoutChanges  定義子佈局新增子View時候是否帶有動畫效果,如果為true,那麼再新增子View時候
會帶有從隱藏到顯示的動畫效果,為false則沒有。如果要改變動畫效果,就得自定義layoutAnimation動畫,通過
android:layoutAnimation這個來引用即可android:descendantFocusability

開發中很常見的一個問題,專案中的listview不僅僅是簡單的文字,常常需要自己定義listview,自己的Adapter去繼承BaseAdapter,在adapter中按照需求進行編寫,問題就出現了,可能會發生點選每一個item的時候沒有反應,無法獲取的焦點。原因多半是由於在你自己定義的Item中存在諸如ImageButton,Button,CheckBox等子控制元件(也可以說是Button或者Checkable的子類控制元件),此時這些子控制元件會將焦點獲取到,所以常常當點選item時變化的是子控制元件,item本身的點選沒有響應。

    這時候就可以使用descendantFocusability來解決啦,API描述如下:

android:descendantFocusability

Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.

Must be one of the following constant values.

該屬性是當一個為view獲取焦點時,定義viewGroup和其子控制元件兩者之間的關係。

屬性的值有三種:

        beforeDescendants:viewgroup會優先其子類控制元件而獲取到焦點

        afterDescendants:viewgroup只有當其子類控制元件不需要獲取焦點時才獲取焦點

        blocksDescendants:viewgroup會覆蓋子類控制元件而直接獲得焦點

通常我們用到的是第三種,即在Item佈局的根佈局加上android:descendantFocusability=”blocksDescendants”的屬性就好了,至此listview點選的靈異事件告一段落。


android:duplicateParentState,當為true時候,會使ViewGroup子類中無法響應點選事件的View的drawable狀態
產生響應的變化,但是它自己本身的drawable狀態不會跟隨著變化,如果要想它本身也隨著變化就得將其置為false即可。
該屬性生效的前提是它是可以點選的,否則不會生效。

android:baselineAligned

那到底這個屬性是做什麼用的呢?

         baselineAligned:基準線對齊。

     首先要解釋什麼是基準線,這個在中文中不常見,但在以字母為書寫語言的其他國家非常常見。

    

      如上圖所示,紅線就是基線(baseline),是不是很熟悉,這不就是我們經常寫英文的四條線中的第三條嗎。

    

     那baselineAligned是做什麼用的呢?根據官方文件,baselineAligned預設設定為true,當設定為false時,

      佈局檔案和它的孩子的基準線不對齊。

      讓我們通過一個例子來看看效果怎樣的。

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"
 5     android:baselineAligned="false"
 6     android:orientation="horizontal">
 7 
 8     <TextView
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_marginRight="5dip"
12         android:text="TextView:p" />
13     <TextView 
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:layout_marginRight="5dip"
17         android:textSize="30sp"
18         android:text="LargeTextView:p"
19         />
20 </LinearLayout>

 這是將baselineAligned值設定為false時,也就是不對齊。看看執行效果:

 

  把baselineAligned值改為true。

 

android:baselineAlignedChildIndex

android:layout_baselinealignedChildIndex屬性 指當前layout是 以哪個view的基準線與

其他的View進行對齊。這個屬性只對Button、TextView、EditView有效且下標從0開始。

android:hapticFeedbackEnabled 設定View是否