温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中怎么利用Layout实现用户界面

发布时间:2021-07-12 11:29:41 来源:亿速云 阅读:163 作者:Leah 栏目:移动开发

今天就跟大家聊聊有关Android中怎么利用Layout实现用户界面,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

1.帧布局 FrameLayout:

FrameLayout是最简单的布局对象。在它里面的的所有显示对象都将固定在屏幕的左上角,不能指定位置,后一个会直接覆盖在前一个之上显示:

Android中怎么利用Layout实现用户界面

如图所示第二个TextView直接覆盖在了第一个TextView上面。

2.线性布局 LinearLayout:

LinearLayout是最常用的布局之一,也是RadioGroup, TabWidget, TableLayout, TableRow, ZoomControls类的父类,它里面所有显示的对象都以垂直或水平的方式排列(通过设置LinearLayout的Orentation属性来设置排列方式):

Android中怎么利用Layout实现用户界面

3.相对布局 RelativeLayout:

RelativeLayout 允许子元素指定它们相对于其父元素或兄弟元素的位置,是实际布局中最常用的布局方式之一。它灵活性大、属性也多,操作难度比较大,属性之间产生冲突的的可能性也大,使用相对布局时需要多做测试。

Android中怎么利用Layout实现用户界面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="20dp"
            android:src="@drawable/test" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/imageView1"
            android:layout_centerHorizontal="true"
            android:text="在imageView1下方"
            android:textSize="15sp" />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/textView1"
            android:layout_centerHorizontal="true"
            android:text="在testView1下方"
            android:textSize="15sp" />
    </RelativeLayout>

RelativeLayout用到的一些重要的属性:
第一类:属性值为true或false
android:layout_centerHrizontal -------------------------------水平居中
android:layout_centerVertical ---------------------------------垂直居中
android:layout_centerInparent --------------------------------相对于父元素完全居中
android:layout_alignParentBottom ----------------------------贴紧父元素的下边缘
android:layout_alignParentLeft --------------------------------贴紧父元素的左边缘
android:layout_alignParentRight ------------------------------贴紧父元素的右边缘
android:layout_alignParentTop --------------------------------贴紧父元素的上边缘
android:layout_alignWithParentIfMissing ----------------------如果对应的兄弟元素找不到的话就以父元素做参照物
第二类:属性值必须为id的引用名“@id/id-name”
android:layout_below -----------------------------------------在某元素的下方
android:layout_above ----------------------------------------在某元素的的上方
android:layout_toLeftOf --------------------------------------在某元素的左边
android:layout_toRightOf -------------------------------------在某元素的右边
android:layout_alignTop --------------------------------------本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft --------------------------------------本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom ----------------------------------本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight -------------------------------------本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如30dip,40px
android:layout_marginBottom --------------------------------离某元素底边缘的距离
android:layout_marginLeft ------------------------------------离某元素左边缘的距离
android:layout_marginRight ----------------------------------离某元素右边缘的距离
android:layout_marginTop ------------------------------------离某元素上边缘的距离

 4.表格布局 TableLayout:

TableLayout以行列的形式管理子元素,每一行是一个TableRow布局对象,当然也可以是普通的View对象,TableRow离每放一个元素就是一列,总列数由列数最多的那一行决定。

Android中怎么利用Layout实现用户界面

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="*" >
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_span="2"
                android:text="第一行合并两列居中"
                android:textSize="20sp" />
        </TableRow>
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第一行第一列" />
            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第一行第二列" />
        </TableRow>
        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第二行第一列" />
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="第二行第二列" />
        </TableRow>
    </TableLayout>

android:layout_span="2"是设置该TextView占据2列(我在界面设计器里面没找到TextView的Span属性,所以是在xml文件里面直接添加的),android:stretchColumns="*"是设置该TableLayout的所有列都自动扩展,如果不设置自动扩展每行列宽会根据显示的内容改变。

Android中怎么利用Layout实现用户界面

TableLayout的几个重要属性:

collapseColumns -----------------------------设置隐藏那些列,列ID从0开始,多个列的话用”,”分隔
stretchColumns ------------------------------设置自动伸展那些列,列ID从0开始,多个列的话用”,”分隔
shrinkColumns -------------------------------设置自动收缩那些列,列ID从0开始,多个列的话用”,”分隔

看完上述内容,你们对Android中怎么利用Layout实现用户界面有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI