【Android佈局】在程序中設置android:gravity 和 android:layo...

在進行UI佈局的時候,可能常常會用到 android:gravity  和 android:layout_Gravity 這兩個屬性。html

 

關於這兩個屬性的區別,網上已經有不少人進行了說明,這邊再簡單說一下。 (資料來自網絡)java

 

 

 

 

LinearLayout有兩個很是類似的屬性:android

android:gravity與android:layout_gravity。網絡

 

他們的區別在於:佈局

 

android:gravity 屬性是對該view中內容的限定.好比一個button 上面的text. 你能夠設置該text 相對於view的靠左,靠右等位置.

android:layout_gravity是用來設置該view相對與父view 的位置.好比一個button 在linearlayout裏,你想把該button放在linearlayout裏靠左、靠右等位置就能夠經過該屬性設置. this

 

即android:gravity用於設置View中內容相對於View組件的對齊方式,而android:layout_gravity用於設置View組件相對於Container的對齊方式。spa

 

原理跟android:paddingLeft、android:layout_marginLeft有點相似。若是在按鈕上同時設置這兩個屬性。.net

android:paddingLeft="30px"  按鈕上設置的內容離按鈕左邊邊界30個像素
android:layout_marginLeft="30px"  整個按鈕離左邊設置的內容30個像素xml

 


下面回到正題, 咱們能夠經過設置android:gravity="center"來讓EditText中的文字在EditText組件中居中顯示;同時咱們設置EditText的android:layout_gravity="right"來讓EditText組件在LinearLayout中居右顯示。看下效果:htm

 

 

 

正如咱們所看到的,在EditText中,其中的文字已經居中顯示了,而EditText組件本身也對齊到了LinearLayout的右側。

 

附上佈局文件:

 

  1. <LinearLayout  
  2.    xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent">  
  6.     <EditText  
  7.         android:layout_width="wrap_content"  
  8.         android:gravity="center"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="one"  
  11.         android:layout_gravity="right"/>  
  12. </LinearLayout>  
<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" android:layout_height="wrap_content" android:text="one" android:layout_gravity="right"/> </LinearLayout> 

 

 

 

那麼上面是經過佈局文件的方式來設置的。,相信你們都曾寫過,那麼如何經過Java代碼來設置組件的位置呢?

 

依然考慮實現上述效果。

 

經過查看SDK,發現有一個setGravity方法, 顧名思義, 這個應該就是用來設置Button組件中文字的對齊方式的方法了。

仔細找了一圈,沒有發現setLayoutgravity方法, 有點失望。 不過想一想也對, 若是這邊有了這個方法, 將Button放在不支持Layout_Gravity屬性的Container中如何是好! 

 

因而想到, 這個屬性有可能在Layout中 , 因而仔細看了看LinearLayout 的 LayoutParams, 果真有所發現, 裏面有一個 gravity 屬性,相信這個就是用來設置組件相對於容器自己的位置了,沒錯,應該就是他了。

 

實踐後發現,若是如此, 附上代碼,各位本身看下。

 

 

 

代碼比較簡單,可是發現它們仍是花了我一點時間的。

 

  1. Button button  = new Button(this);  
  2. button.setText("One");  
  3. LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
  4. //此處至關於佈局文件中的Android:layout_gravity屬性   
  5. lp.gravity = Gravity.RIGHT;  
  6. button.setLayoutParams(lp);  
  7. //此處至關於佈局文件中的Android:gravity屬性   
  8. button.setGravity(Gravity.CENTER);  
  9.   
  10. LinearLayout linear = new LinearLayout(this);  
  11. //注意,對於LinearLayout佈局來講,設置橫向仍是縱向是必須的!不然就看不到效果了。   
  12. linear.setOrientation(LinearLayout.VERTICAL);  
  13. linear.addView(button);  
  14. setContentView(linear);  
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); 

 

或者這樣也能夠:

 

  1. Button button  = new Button(this);  
  2. button.setText("One");  
  3. //此處至關於佈局文件中的Android:gravity屬性   
  4. button.setGravity(Gravity.CENTER);  
  5.   
  6. LinearLayout linear = new LinearLayout(this);  
  7. //注意,對於LinearLayout佈局來講,設置橫向仍是縱向是必須的!不然就看不到效果了。   
  8. linear.setOrientation(LinearLayout.VERTICAL);  
  9.   
  10. LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
  11. //此處至關於佈局文件中的Android:layout_gravity屬性   
  12. lp.gravity = Gravity.RIGHT;  
  13.   
  14. linear.addView(button, lp);  
  15. setContentView(linear);  
Button button = new Button(this); button.setText("One"); //此處至關於佈局文件中的Android:gravity屬性 button.setGravity(Gravity.CENTER); LinearLayout linear = new LinearLayout(this); //注意,對於LinearLayout佈局來講,設置橫向仍是縱向是必須的!不然就看不到效果了。 linear.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //此處至關於佈局文件中的Android:layout_gravity屬性 lp.gravity = Gravity.RIGHT; linear.addView(button, lp); setContentView(linear); 

 

好了,效果圖就不上了,跟上面的同樣。 就講這麼多。

相關文章
相關標籤/搜索