標籤:android http io ar div line res ef
gravity用來描述當前view的內容在view中的位置。
例如 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="bottom" android:text="hello" />那麼hello就會出現在按鈕的最下方。 如果當前view是個view group,例如<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello" /> </LinearLayout>那按鈕會出現在layout的最下方。對這個例子,也就是螢幕的最下方。 如果把button的寬高都改為match_parent那對於 <Button android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom" android:text="hello" />button跟layout一樣大,hello是在該button的最下方。對於<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom" > <Button android:layout_width="match_parent" android:layout_height="match_parent" android:text="hello" /> </LinearLayout>因為現在button和layouty一樣大,所以LinearLayout中的android:gravity沒有實際顯示效果。另外,該屬性無遞迴效果。 gravity是控制其內容或者包含的views在該view(或view group)中的位置。而layout_gravity是表示該view在其父容器view group中的位置。該屬性只在父容器是LinearLayout和FrameLayout時有效。如果要實現button出現在LinearLayout的最下方(上面第二個例子),用layout_gravity可以這麼寫<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:layout_gravity="bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hello" /> </LinearLayout> 只需要把LinearLayout中的android:gravity="bottom"刪掉,然後再Button加入android:layout_gravity="bottom" 如果把上面例子中的android:orientation=”horizontal"改為vertical,那麼button位置還是在上面。這是因為對於LinearLayout,如果是horizontal,則只能採用top,center,bottom等上下結構的屬性控制縱向位置;如果是vertical,則只能控制橫向位置。 與gravity一樣,如果該view和父容器view group的大小一樣,則該屬性無實際效果。事實上通過wrap_content和match_parent,這種情況經常出現。所以經常發現gravity和layout_gravity無效的情況。這時就要檢查是否是因為父子view大小相同了。