Android Layout布局摘要(备忘一)

来源:互联网 发布:站外淘宝精选怎么加入 编辑:程序博客网 时间:2024/06/05 07:03

简述:

界面设计,布局以及控件吃的使用 对于一款应用软件很重要,这里简要阐述几个使用布局以及控件,以及每个他们部常用的参数设置

(各个布局的属性资料从网上收集)浓缩之后,方便统一查看, 有一些重复的属性如果已经列出,不再重复列举


首先记录一下Android的单位,

px  : 屏幕上的点

in: 英寸

mm: 毫米

pt : 1/72英寸

dp:   与密度无关的像素,一种基于屏幕密度的抽象单位。在每英寸160点的显示器上1dp = 1px

sp:   根据用户字体大小首选项进行缩放(以此作为文字大小的单位比较合适)



一. 布局

1.  LinearLayout

android:orientation= "vertical"    表示竖直方式对齐,

代码中: 使用setOrientation(LinearLayout.VERTICAL)设置该属性;

android:orientation="horizontal"  表示水平方式对齐

android:paddingLeft="10dip"   以下四个表示到外围边框的距离,padding表示与父view的距离(在父控件处定义)

android:paddingRight="10dip"

android:paddingTop="10dip"

android:paddingBottom="10dip"

android:layout_width="fill_parent" 定义当前视图在屏幕上可以使用的宽度,fill_parent即填充(父空间)若是顶层,则填充整个屏幕

android:layout_height="fill_parent" 随着文字栏位置的不同而改变这个视图的宽度或者高度。自动设置宽度或者高度

android:layout_weight          

1) layout_width=fill_parent”时,其权重赋值遵守数值越小,首要度越高的原则

2) layout_width=wrap_content”时,其值默示占用残剩空间的比例。

用来表示占整行(orientation 为horizontal)或者整列(vertical) 的权重

下面是一段测试

大的layout我选用LinearLayout

layout_width="fill_parent"

<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"     >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="@color/aquamarine"        android:text="hello"        android:gravity="center"        android:textColor="@android:color/black" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="2"        android:background="@color/beige"        android:text="Android"        android:gravity="center"        android:textColor="@android:color/black" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_weight="3"        android:background="@color/purple"        android:text="world"        android:gravity="center"        android:textColor="@android:color/black" /></LinearLayout>

效果如下,

发现layout_weight 越小,占得比重越大,layout_weight为3的基本就没了。。


如果linearLayout中,各个控件的layout_width 改为wrap_content 又会变成怎么样呢

<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"     >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="1"        android:background="@color/aquamarine"        android:text="hello1"        android:gravity="center"        android:textColor="@android:color/black" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="2"        android:background="@color/beige"        android:text="hello2"        android:gravity="center"        android:textColor="@android:color/black" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="3"        android:background="@color/purple"        android:text="hello3"        android:gravity="center"        android:textColor="@android:color/black" /></LinearLayout>

发现这里的结果完全按照weight的大小来区分的,谁的layout_weight值大,那么他所占的空间就大



android:background="#0000aa"       背景可以为一种颜色或者一张图片 android:background="@drawable/bg_img"

android:text="Hello"  指定字体,也可以引用String.xml 例如:android:text="@string/hello"



2. TableLayout

 常会用到<TableRow> </TableRow> 表示一行

此外TableLayout中还有几个属性,需要在这里测试一下

android:stretchColumns="0" 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:stretchColumns="0" >    <TableRow>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@color/aquamarine"            android:gravity="center"            android:text="hello1"            android:textColor="@android:color/black" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@color/beige"            android:gravity="center"            android:text="hello2"            android:textColor="@android:color/black" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@color/purple"            android:gravity="center"            android:text="hello3"            android:textColor="@android:color/black" />    </TableRow></TableLayout>

发现第0列,被拉伸了


之后再测试

android:collapseColumns="0"

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:collapseColumns="0" >    <TableRow>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@color/aquamarine"            android:gravity="center"            android:text="hello1"            android:textColor="@android:color/black" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@color/beige"            android:gravity="center"            android:text="hello2"            android:textColor="@android:color/black" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@color/purple"            android:gravity="center"            android:text="hello3"            android:textColor="@android:color/black" />    </TableRow></TableLayout>


发现第0列没了, 这就是collapseColumns的效果 , columns的几个属性同时制定多列,用逗号,隔开


现在测试

android:shrinkColumns="0"

首先是没有shrink时候的效果

加了shrinkColumns之后

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="300dip"    android:layout_height="fill_parent"    android:shrinkColumns="0" >    <TableRow>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@color/aquamarine"            android:gravity="center"            android:text="hello1"            android:textColor="@android:color/black" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@color/beige"            android:gravity="center"            android:text="hello2"            android:textColor="@android:color/black" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:background="@color/purple"            android:gravity="center"            android:text="hello3"            android:textColor="@android:color/black" />    </TableRow></TableLayout>


从效果可以得知, shrink会自动使得第0列坍缩(所以如果整行宽度足够的话就不会坍缩), 直到该列宽度为零为止



二. 控件:

1. TextView

1)android:padding="3dip"    规定自己和其他(上下左右)的view之间的距离,如果只有一个View那么和padding 功能相同

指向性的margin

android:layout_marginBottom="10dip" 

android:layout_marginLeft="10dip"

android:layout_marginTop="10dip" 

android:layout_marginRight="10dip"


2) android:visibility

可见(visible)
XML文件:android:visibility="visible"
Java代码:view.setVisibility(View.VISIBLE);

不可见(invisible)
XML文件:android:visibility="invisible"
Java代码:view.setVisibility(View.INVISIBLE);

隐藏(GONE)
XML文件:android:visibility="gone"
Java代码:view.setVisibility(View.GONE);

INVISIBLE和GONE的主要区别是:

当控件visibility属性为INVISIBLE时,界面保留了view控件所占有的空间;

而控件属性为GONE时,界面则不保留view控件所占有的空间。


原创粉丝点击