|
android:layout_marginLeft指該控件距離邊父控件的邊距,
android:paddingLeft指該控件內部內容,如文本距離該控件的邊距。
如:
當按鈕分別設置以上兩個屬性時,得到的效果是不一樣的。
android:paddingLeft="30px":
按鈕上設置的內容(例如圖片)離按鈕左邊邊界30個像素。
android:layout_marginLeft="30px"
整個按鈕離左邊設置的內容30個像素
這二個屬性是相對的,假設B是A的子控件,設置B的margin和設置A的padding能達到相同的效果。
-------------------------------------------------- ----------------
padding是站在父view的角度描述問題,它規定它裡面的內容必須與這個父view邊界的距離。
margin則是站在自己的角度描述問題,規定自己和其他(上下左右)的view之間的距離,如果同一級只有一個view,那麼它的效果基本上就和padding一樣
-------------------------------------------------- ---------------
android:layout_marginLeft當前控件左側的留白
android:layout_marginRight當前控件右側的留白
android:layout_marginTop當前控件上方的留白
android:layout_marginBottom當前控件下方的留白
android:layout_margin當前控件上下左右四個方向的留白
-------------------------------------------------- ---------------
android:gravity / android:layout_Gravity區別:
android:gravity 是設置該view裡面的內容相對於該view的位置,例如設置button裡面的text相對於view的靠左,居中等位置。
android:layout_gravity 是用來設置該view相對與父view 的位置,例如設置button在layout裡面的相對位置:屏幕居中,水平居中等。
在實踐中,發現有時會同時使用android:gravity和 android:layout_Gravity:
EditText
- <LinearLayout
- xmlns:android="HTTP://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <EditText
- android:layout_width="wrap_content"
- android:gravity="center" // 設置文字在TextView的居中位置
- android:layout_height="wrap_content"
- android:text="one"
- android:layout_gravity="right"/> // 設置EditText位置(Layout):屏幕靠右
- </LinearLayout>
-
複製代碼
通過設置android:gravity="center"來讓EditText中的文字在EditText組件中居中顯示;同時我們設置EditText的android:layout_gravity="right"來讓EditText組件在LinearLayout中居右顯示。看下效果:
而Java代碼中設置android:gravity是極其簡單的,setGravity函數搞定了:
//此處相當於佈局文件中的Android:gravity屬性 button. setGravity (Gravity.CENTER);
--------------------------------------------------------------------------------
那麼上面是通過佈局檔的方式來設置的。,相信大家都曾寫過,那麼如何通過JAVA代碼來設置組件的位置呢?
依然考慮實現上述效果。
通過查看SDK,發現有一個setGravity方法, 顧名思義, 這個應該就是用來設置Button組件中文字的對齊方式的方法了。
仔細找了一圈,沒有發現setLayoutgravity方法, 有點失望。 不過想想也對, 如果這邊有了這個方法, 將Button放在不支援Layout_Gravity屬性的Container中如何是好!
於是想到, 這個屬性有可能在Layout中 , 於是仔細看了看LinearLayout 的 LayoutParams, 果然有所發現, 裏面有一個 gravity 屬性,相信這個就是用來設置組件相對於容器本身的位置了,沒錯,應該就是他了。
代碼
- Button button = new Button(this);
- button.setText("One");
- LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
- //此處相當於佈局檔中的Android:layout_gravity屬性
- lp.gravity = Gravity.RIGHT;
- button.setLayoutParams(lp);
- //此處相當於佈局檔中的Android:gravity屬性
- button.setGravity(Gravity.CENTER);
- LinearLayout linear = new LinearLayout(this);
- //注意,對於LinearLayout佈局來說,設置橫向還是縱向是必須的!否則就看不到效果了。
- linear.setOrientation(LinearLayout.VERTICAL);
- linear.addView(button);
- setContentView(linear);
複製代碼
但設置android:layout_margin就麻煩些:
- FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
- //此處相當於佈局文件中的Android:layout_gravity屬性
- lp.gravity = Gravity.CENTER;
- mTextDisplayed.setLayoutParams(lp);
複製代碼
android:padding / android:layout_margin區別:android:padding 是指該view裡面的內容與view邊界的距離,例如TextView裡面的文字離TextView控件邊界的數值;
android:layout_margin 是指該view於父view邊界的距離,例如SeekBar與父view的四周邊界。
|
|