Android studio最新水平居中和垂直居中(全)
2017-07-08 11:03
447 查看
网上很多帖子的居中都是垂直剧终,下面介绍两种居中方式
居中呢,这里分两种不同布局方式的居中!分别是 LinearLayout 和
- 首先说的是LinearLayout布局下的居中:直接贴源码
LinearLayout又分为水平分布还是垂直分布 :
[html] view plain copy
print?
<LinearLayout
// 下面gravity属性的参数:center为居中,center_horizontal为水平居中,center_vertical为垂直居中 删除其他的就能得到自己想要的
android:gravity="center|center_horizontal|center_vertical" >
- 然后说的是
[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%/此设置的数量,就是占据多少
比如:
<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
居中呢,这里分两种不同布局方式的居中!分别是 LinearLayout 和
RelativeLayout。[/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" >
- 然后说的是
RelativeLayout布局下的居中:
[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“ :放在容器的中心
相关文章推荐
- android textview字体加粗 Android studio最新水平居中和垂直居中
- 实现文本垂直居中水平居中的几种方法
- 盘点8种CSS实现垂直居中水平居中的绝对定位居中技术
- 在固定尺寸的盒子,水平垂直居中不知宽高的图片
- [分享] 纯CSS完美实现垂直水平居中的6种方式
- POI导出EXCEL文档、水平垂直居中、表头添加颜色
- CSS实现水平垂直同时居中的5种思路
- 基于jQuery实现的水平和垂直居中的div窗口
- CSS水平垂直居中的几种方法
- 元素水平垂直居中的几种方法
- 控件水平或垂直居中在布局中的问题
- css如何让文字水平垂直居中
- iOS UITextView 垂直水平居中
- 让层水平垂直居中页面的两种方法
- css3实现元素水平垂直居中
- div中图片水平垂直居中
- 七种CSS方式让一个容器水平垂直居中
- CSS 实现:文字水平垂直居中
- 使用inline-block做水平垂直居中
- DIV教程之如何使div居中(垂直水平居中)(转载于www.jqueryajax.com)