今天學習安卓第一行代碼時,用gravity=“center”把textview中的內容居中顯示,結果誤打成了layout_gravity=“center”,運行了一下,發現文字位置沒有發生變化。
論壇查了下,發現兩者天差地別。
gravity是決定控制元件的內容如何在控制元件內定位。
layout_gravity是決定控制元件如何在其父布局內定位。
附圖

uj5u.com熱心網友回復:
gravity,自居中,控制元件中的內容會以當前控制元件為母體居中顯示,layout_gravity該控制元件相對于父布局的位置,當然父布局如果設定居中,就得先以子布局的位置優先,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/dp_12"
android:text="ddddd"
android:gravity="center"
android:layout_gravity="bottom"/>
</LinearLayout>

,不是所有的布局都有效,RelativeLayout、FrameLayout,都是根據相對位置,或者幀壓上去的,具體問題
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/111302.html
標籤:Android
上一篇:為什么應用打開后無法顯示?
下一篇:android emulator
