线性布局,表格布局、帧布局和相对布局的介绍参看上一篇博文: Android的布局管理器(上篇)
网格布局
GridLayout的常用属性GridLayoutLayoutParams常用属性xml示例 绝对布局
xml属性 Android单位与换算
单位案例
网格布局绝对布局
网格布局
网格布局由GridLayout类表示,它是Android4.0新增的布局管理器。如果希望在更早的Android平台上使用该布局管理器,则需要导入相应支撑库。GridLayout的作用类似HTML中的table标签,它把整个容器划分成rows
×
Columns个网格,每个网格可以放置一个组件,除此之外也可以设置一个组件横跨多少列,一个组件纵跨多少行。
参考:浅谈android4.0开发之GridLayout布局 如果需要设置某控件跨越多行或多列,只需将该子控件的android:layout_rowSpan或者layout_columnSpan属性设置为数值,再设置其layout_gravity属性为fill即可,前一个设置表明该控件跨越的行数或列数,后一个设置表明该控件填满所跨越的整行或整列。
GridLayout的常用属性
android:alignmentMode 设置该布局管理器采用的对齐模式android:columnCount 设置该网格的列数量android:columnOrderPreserved 设置该网格布局管理器是否保留列序号android:rowCount 设置该网格的行数目android:rowOrderPreserved 设置该网格布局管理器是否保留行序号android:useDefaultMargins 设置布局管理器是否使用默认的页边距
GridLayout.LayoutParams常用属性
android:layout_column 设置该子组件在GridLayout的第几列android:layout_columnSpan 设置该子组件在GridLayout横向上跨几行android:layout_gravity 设置该子组件采用何种方式占据该网格的空间android:layout_row 设置该子组件在GridLayout的第几行android:layout_rowSpan 设置该子组件在GridLayout纵向上跨几行
xml示例
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridLayout
android:columnCount="4"
android:rowCount="7"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:paddingBottom="30dp"
android:paddingLeft="16dp"
android:paddingTop="30dp"
android:text="22 * 10"
android:textSize="30sp"
android:textStyle="bold" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:layout_gravity="right"
android:layout_margin="5dp"
android:text="clear" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_margin="5dp"
android:layout_row="2"
android:background="#fff8f8"
android:text="7" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_margin="5dp"
android:layout_row="2"
android:background="#fde3e3"
android:text="8" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_row="2"
android:background="#ffd9d9"
android:text="9" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_gravity="right"
android:layout_margin="5dp"
android:layout_row="2"
android:background="#ffc6c6"
android:text="/" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_margin="5dp"
android:layout_row="3"
android:background="#fff8f8"
android:text="4" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_margin="5dp"
android:layout_row="3"
android:background="#ffdbfb"
android:text="5" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_row="3"
android:background="#ffc5f9"
android:text="6" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_gravity="right"
android:layout_margin="5dp"
android:layout_row="3"
android:background="#ff96f3"
android:text="*" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_margin="5dp"
android:layout_row="4"
android:background="#fff8f8"
android:text="1" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_margin="5dp"
android:layout_row="4"
android:background="#fdcdce"
android:text="2" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_row="4"
android:background="#ffbdbe"
android:text="3" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_gravity="right"
android:layout_margin="5dp"
android:layout_row="4"
android:background="#fd8789"
android:text="-" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="5"
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:layout_margin="5dp"
android:background="#d9eaff"
android:text="0" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_row="5"
android:background="#bbd9ff"
android:text="." />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#585858"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_column="0"
android:layout_margin="5dp"
android:layout_row="6"
android:background="#7bedff"
android:text="=" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="2"
android:layout_column="3"
android:layout_gravity="fill"
android:layout_margin="5dp"
android:layout_row="5"
android:background="#7dcfff"
android:text="+" />
</GridLayout>
</LinearLayout>
绝对布局
绝对布局由AbsoluteLayout表示,对于AbsoluteLayout布局Android不提供任何控制,而是由开发人员自己用过X坐标和Y坐标来控制组件的位置,以及其他的一些属性。大部分时候,使用绝对布局都不是一个好思路,因为运行Android应用的手机往往千差万别,因此屏幕分辨率,大小都可能存在较大差异,使用绝对布局会很难兼顾不同屏幕分辨率,大小不同的问题,因此
AbsoluteLayout已经过时。
xml属性
android:layout_x 指定该子组件的x坐标 android:layout_y 指定该子组件的y坐标
Android单位与换算
详细可参考:关于 Android 适配,看完这篇文章就够了
单位
px(像素):每一个px对应屏幕上的一点in:表示英寸,每英寸相当于2.54厘米dpi(屏幕像素密度):每英寸的像素点数比如120dpi、160dpi等,其值与屏幕尺寸和分辨率有关dp/dip(设备独立像素):密度无关像素,以160dpi为基准。如求xdip对应的像素值y:
dpi160×x=y
案例
一个手机屏幕分辨率为480*800,屏幕尺寸为3.7in。求其dpi?在布局中宽设为320dp时相当于多少px?
dpi:
4802+8002√3.7=252dpi
320dp:
252160×320=504dp
然而手机屏幕才480px,算出的结果却是504px! 注意: 手机上面计算dpi为理论值,实际上只有120(low),160(medium),240(high),320(xhigh)等几种,因此实际的计算公式为:320
×240160=480px
,与屏幕宽度相同,刚好占据整个屏幕。
转载请注明原文地址: https://ju.6miu.com/read-11695.html