New UI-gravity和layout_gravity屬性解析,uigravitybehavior

來源:互聯網
上載者:User

New UI-gravity和layout_gravity屬性解析,uigravitybehavior

New UI-gravity和layout_gravity屬性解析

 ——轉載請註明出處:coder-pig,歡迎轉載,請勿用於商業用途!


小豬Android開發交流群已建立,歡迎大家加入,無論是新手,菜鳥,大神都可以,小豬一個人的

力量畢竟是有限的,寫出來的東西肯定會有很多紕漏不足,歡迎大家指出,集思廣益,讓小豬的博文

更加的詳盡,幫到更多的人,O(∩_∩)O謝謝!

小豬Android開發交流群:小豬Android開發交流群群號:421858269

新Android UI執行個體大全目錄:http://blog.csdn.net/coder_pig/article/details/42145907




1)兩者用法示範:

相信大家對於這兩個屬性都不陌生吧,也是這兩個其實平時用的很多的:

android:gravity:設定view中內容的對齊!

android:layout_gravity:設定view在版面配置容器中的對齊,要在LinearLayout裡面才可以用哦!


多說無益,通過一個簡單的例子就可以知道了

兩個簡單的TextView,設定不同的gravity和layout_gravity屬性


布局代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.jay.example.getscreendemo.MainActivity"    android:orientation="vertical" >    <TextView        android:layout_gravity="right"        android:background="#FF7878"        android:gravity="center"        android:layout_width="300dp"        android:layout_height="200dp"        android:text="O(∩_∩)O哈哈~"         android:textSize="18sp"/>        <TextView        android:background="#FF7428"        android:gravity="left|bottom"        android:layout_width="300dp"        android:layout_height="200dp"        android:text="(*^__^*) 嘻嘻……"        android:textSize="18sp" /></LinearLayout><strong></strong>

用法都是很簡單的,可選的值都是一樣的,當然可以同時設定多個屬性,只需用|間隔即可,比如左上left|top




2)使用Layout_gravity的一個很重要的問題!!!

這個問題是一個讀者偶爾一次發現反饋給我的,萬分感謝,同時希望大家在看小豬部落格的時候可以提出

一些實際開發中遇到的問題,以及解決方式,好給後來者經驗!畢竟小豬不是神,不是什麼方方面面都能

考慮到的,謝謝!


問題內容:

在一個LinearLayout的水平方向中布置兩個TextView,想讓一個左,一個右,怎麼搞?

或許你會脫口而出:"gravity設定一個left,一個right就可以啦!"

真的這麼簡單?你試過嗎?寫個簡單的Layout你就會發現,事與願違了:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    tools:context="com.jay.example.getscreendemo.MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="200dp"        android:layout_gravity="left"        android:background="#FF7878"        android:gravity="center"        android:text="O(∩_∩)O哈哈~" />    <TextView        android:layout_width="wrap_content"        android:layout_height="200dp"        android:layout_gravity="right"        android:background="#FF7428"        android:gravity="center"        android:text="(*^__^*) 嘻嘻……" /></LinearLayout>
運行結果圖:


看到這裡你會說:哎呀,真的不行耶,要不在外層LinearLayout加個gravity=left的屬性,然後設定第二個

TextView的layout_gravity為right,恩,好我們試一下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"    android:gravity="left"    tools:context="com.jay.example.getscreendemo.MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="200dp"        android:background="#FF7878"        android:gravity="center"        android:text="O(∩_∩)O哈哈~" />    <TextView        android:layout_width="wrap_content"        android:layout_height="200dp"        android:layout_gravity="right"        android:background="#FF7428"        android:gravity="center"        android:text="(*^__^*) 嘻嘻……" /></LinearLayout>
結果還是一樣:



好吧,沒轍了,怎麼辦好?

當 android:orientation="vertical" 時, 只有水平方向的設定才起作用,垂直方向的設定不起作用。

即:left,right,center_horizontal 是生效的。

當 android:orientation="horizontal" 時, 只有垂直方向的設定才起作用,水平方向的設定不起作用。

即:top,bottom,center_vertical 是生效的


不過貌似這個解決方案有點坑爹,比如如果只能豎直方向設定左靠右對齊的話,就會出現下面的效果:


顯然不是我們要的結果把!

綜上,要麼按照上述給出的規則來布局,不過對於這種情況還是使用相對布局RelativeLayout把!

網上沒給出具體的原因,都是說這樣改有人說這個和orientation的優先順序有關

,暫且先mark下來吧,後續如果知道原因的話再解釋!前面螢幕適配也說過了,布局還是建議使用

RelativeLayout!











聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.