Android studio最新水平居中和垂直居中(全)


网上很多帖子的居中都是垂直剧终,下面介绍两种居中方式

居中呢,这里分两种不同布局方式的居中!分别是 LinearLayout 和RelativeLayout。

- 首先说的是LinearLayout布局下的居中:直接贴源码

LinearLayout又分为水平分布还是垂直分布  :

android:orientation="vertical"   //垂直分布
android:orientation="horizontal"  //居中分布

[html]  view plain  copy
 print ?
  1. <LinearLayout  
  2.   // 下面gravity属性的参数:center为居中,center_horizontal为水平居中,center_vertical为垂直居中 删除其他的就能得到自己想要的 
  3.     android:gravity="center|center_horizontal|center_vertical" >  

 

- 然后说的是RelativeLayout布局下的居中:

 

[html]  view plain  copy
 print ?
  1. <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  2.   
  3.     Android:layout_width="match_parent" Android:layout_height="match_parent">  
  4.   
  5.   
  6.   
  7.     <Button Android:id="@+id/btngal" Android:layout_width="wrap_content"  
  8.   
  9.         Android:layout_height="wrap_content" Android:gravity="center_horizontal"  
  10.   
  11.         Android:textSize="20sp" Android:layout_alignParentBottom="true"  
  12.   
  13.         Android:layout_centerHorizontal="true" Android:text="返回主界面" />  
  14.   
  15. </RelativeLayout>   

 

 

简单说明

占据页面百分之多少,100%/此设置的数量,就是占据多少

android:layout_weight="1"
比如:

<LinearLayout
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="男"
            android:textSize="20dp"
            android:id="@+id/nan"
            android:layout_weight="1"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="20dp"
            android:id="@+id/nv"
            android:layout_weight="1"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="人妖"
            android:textSize="20dp"
            android:layout_weight="1"/>
    </LinearLayout>

总共设置了3个 android:layout_weight="1",也就是男、女、人妖个占据百分之30



Android:gravity="CENTER_VERTICAL“:这个是垂直居中对齐

Android:gravity="BOTTOM”:放在容器的底部

Android:gravity="CENTER“ :放在容器的中心

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值