android:gravity 和 android:layout_Gravity属性 的区别

本文详细解释了Android开发中android:gravity和android:layout_gravity属性的区别,并提供了通过XML布局文件和Java代码设置这些属性的具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在进行UI布局的时候,可能经常会用到 android:gravity  和 android:layout_Gravity 这两个属性。

 

关于这两个属性的区别,网上已经有很多人进行了说明,这边再简单说一下。 (资料来自网络)

 

 

 


 

LinearLayout有两个非常相似的属性:

android:gravity与android:layout_gravity。

 

他们的区别在于:

 

android:gravity 属性是对该view中内容的限定.比如一个button 上面的text. 你可以设置该text 相对于view的靠左,靠右等位置.

android:layout_gravity是用来设置该view相对与父view 的位置.比如一个button 在linearlayout里,你想把该button放在linearlayout里靠左、靠右等位置就可以通过该属性设置. 

 

即android:gravity用于设置View中内容相对于View组件的对齐方式,而android:layout_gravity用于设置View组件相对于Container的对齐方式。

 

原理跟android:paddingLeft、android:layout_marginLeft有点类似。如果在按钮上同时设置这两个属性。

android:paddingLeft="30px"  按钮上设置的内容离按钮左边边界30个像素
android:layout_marginLeft="30px"  整个按钮离左边设置的内容30个像素

 


下面回到正题, 我们可以通过设置android:gravity="center"来让EditText中的文字在EditText组件中居中显示;同时我们设置EditText的android:layout_gravity="right"来让EditText组件在LinearLayout中居右显示。看下效果:

 

 

 

正如我们所看到的,在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>  
 

 

 


 

那么上面是通过布局文件的方式来设置的。,相信大家都曾写过,那么如何通过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);  
 

 

或者这样也可以:

 


  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);  
 

 

好了,效果图就不上了,跟上面的一样。 就讲这么多。

 

另外,要设置在RelativeLayout中的位置时使用addRule方法,如下:


  1. params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  2.         params.addRule(RelativeLayout.CENTER_IN_PARENT);  
  3.         mContainer.addView(progress,params); 
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff2f2f2" tools:context="com.hik.netsdk.SimpleDemo.View.MainActivity"> <RelativeLayout android:id="@+id/ra_title" android:layout_width="match_parent" android:layout_height="44dp" android:background="@mipmap/title_bg"> <TextView android:id="@+id/titlename" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center" android:text="易丰视频监控" android:textColor="@android:color/white" android:textSize="18sp" /> <ImageButton android:id="@+id/back" android:layout_width="40dp" android:layout_height="match_parent" android:background="@null" android:paddingLeft="10dp" android:contentDescription="视频插件" android:src="@mipmap/img_back" /> <ImageButton android:id="@+id/ib_rotate" android:layout_width="50dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:onClick="changeScreen" android:layout_marginRight="6dp" android:scaleType="centerInside" android:background="@drawable/gps_select" android:contentDescription="视频插件" android:src="@mipmap/ic_size_sel" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_control" android:layout_width="match_parent" android:layout_height="266dp" android:layout_alignParentBottom="true" > <LinearLayout android:id="@+id/ll_center" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerInParent="true" android:background="@mipmap/ycjk_yp" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb1" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb2" /> <ImageButton android:visibility="gone" android:id="@+id/left_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@null" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb3" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb4" /> </LinearLayout> <ImageButton android:id="@+id/ptz_top_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@drawable/nnew_video_up" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb1" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb3" /> <ImageButton android:visibility="gone" android:id="@+id/right_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb2" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb4" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" > <ImageButton android:id="@+id/ptz_left_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@drawable/nnew_video_left" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_zj" /> <ImageButton android:id="@+id/ptz_right_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:contentDescription="视频插件" android:background="@drawable/nnew_video_right" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb4" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb2" /> <ImageButton android:visibility="gone" android:id="@+id/left_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb3" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb1" /> </LinearLayout> <ImageButton android:id="@+id/ptz_bottom_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@drawable/nnew_video_down" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb4" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:contentDescription="视频插件" android:background="@mipmap/ycjk_kb3" /> <ImageButton android:visibility="gone" android:id="@+id/right_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@null" /> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@mipmap/ycjk_kb2" /> </LinearLayout> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:minWidth="48dp" android:minHeight="48dp" android:padding="12dp" android:background="@mipmap/ycjk_kb1" /> </LinearLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toLeftOf="@id/ll_center" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical"> <ImageButton android:id="@+id/focus_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:textSize="16dp" android:text="焦距 +" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/guangquan_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more3" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="光圈 +" android:contentDescription="视频插件" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/zoom_add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more5" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="变倍 +" android:textSize="16dp" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@id/ll_center" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/foucus_reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="焦距 -" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/guangquan_reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more4" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="光圈 -" android:contentDescription="视频插件" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" > <ImageButton android:id="@+id/zoom_reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more6" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/black" android:text="变倍 -" android:textSize="16dp" /> </LinearLayout> </LinearLayout> </RelativeLayout> <RelativeLayout android:layout_above="@id/rl_control" android:layout_below="@id/ra_title" android:layout_width="match_parent" android:layout_height="match_parent" > <SurfaceView android:id="@+id/realplay_sv" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent" /> <ImageButton android:id="@+id/ib_rotate2" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentLeft="true" android:layout_marginLeft="3dp" android:background="@color/green" android:onClick="changeScreen" android:contentDescription="视频插件" android:src="@mipmap/img_systems_close" /> <ProgressBar android:id="@+id/liveProgressBar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> <LinearLayout android:id="@+id/ll_hc" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" > <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/ptz_top_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@mipmap/h_up" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/ptz_bottom_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@mipmap/h_down" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/ptz_left_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@mipmap/h_left" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/ptz_right_btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@mipmap/h_right" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1" > <ImageButton android:id="@+id/focus_add2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more1" /> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <ImageButton android:id="@+id/foucus_reduce2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more2" /> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <ImageButton android:id="@+id/zoom_add2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more5" /> </LinearLayout> <LinearLayout android:gravity="center" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > <ImageButton android:id="@+id/zoom_reduce2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="视频插件" android:background="@drawable/video_more6" /> </LinearLayout> </LinearLayout> </RelativeLayout> </RelativeLayout> 依据上述代码解决报错 <ImageButton>: Touch target size too small <ImageButton>: Touch target size too small <ImageButton>: Touch target size too small <ImageButton>: Touch target size too small <ImageButton>: Touch target size too small
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值