【整理】Android中的gravity和layout_gravity区别

时间:2015-05-03 23:38:28   收藏:0   阅读:340


    

【背景】

在Android中,想要设置个按钮的水平对齐,都累死了:

【已解决】ADT中已设置TableLayout布局的情况下如何设置按钮居中对齐    所以现在有必要搞清楚,到底gravity和layout_gravity到底有啥区别。


1.参考:

Android – gravity and layout_gravity

Android中gravity与layout_gravity的区别

中的解释,可以总结为:

<TableRow
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:gravity="right">

    <Button
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:gravity="center"
        android:layout_gravity="center"
        android:onClick="preformDownload"
        android:text="@string/btn_download" />
</TableRow>
 	<TableRow
	    android:layout_width="fill_parent" 
	    android:layout_height="fill_parent"
	    android:gravity="center">

 	    <Button
 	        android:layout_width="fill_parent" 
 	        android:layout_height="fill_parent"
 	        android:gravity="center"
 	        android:layout_gravity="center"
 	        android:onClick="preformDownload"
 	        android:text="@string/btn_download" />
 	</TableRow>
 	    <Button
 	        android:layout_width="fill_parent"
 	        android:layout_height="fill_parent"
 	        android:layout_gravity="bottom"
 	        android:gravity="top"
 	        android:onClick="preformDownload"
 	        android:text="@string/btn_download" />
 	    <Button
 	        android:layout_width="fill_parent"
 	        android:layout_height="fill_parent"
 	        android:layout_gravity="bottom"
 	        android:gravity="center_vertical"
 	        android:onClick="preformDownload"
 	        android:text="@string/btn_download" />
 	<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:layout_height="wrap_content"
	        android:layout_gravity="center"
	        android:gravity="center"
	        android:text="one" />

	</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:layout_height="wrap_content"
	        android:layout_gravity="right"
	        android:gravity="center"
	        android:text="one" />

	</LinearLayout>

 

【总结】

总之,拿着代码,多试试,就容易理解了。

原文:http://www.cnblogs.com/qqhfeng/p/4474920.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!