您的位置:首页 > 移动开发 > Android开发

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

2017-07-08 11:03 447 查看
网上很多帖子的居中都是垂直剧终,下面介绍两种居中方式

居中呢,这里分两种不同布局方式的居中!分别是 LinearLayout 和
Relative
Layout。[/code]

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

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

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


[html] view plain copy

 print?

<LinearLayout  
  // 下面gravity属性的参数:center为居中,center_horizontal为水平居中,center_vertical为垂直居中 删除其他的就能得到自己想要的 
    android:gravity="center|center_horizontal|center_vertical" >  

 

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

 

[html] view plain copy

 print?

<RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  
    Android:layout_width="match_parent" Android:layout_height="match_parent">  
  
  
  
    <Button Android:id="@+id/btngal" Android:layout_width="wrap_content"  
  
        Android:layout_height="wrap_content" Android:gravity="center_horizontal"  
  
        Android:textSize="20sp" Android:layout_alignParentBottom="true"  
  
        Android:layout_centerHorizontal="true" Android:text="返回主界面" />  
  
</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“ :放在容器的中心
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: