Android布局和控件

一.概述

通过学习本篇文章,你将学会:
1.Android的五大常用布局及其用法
2.shape样式的使用
3.选择器的使用

二.Android五大布局和使用

Android六大布局如下图所示:


image.png

从图中可以看到传统布局包括:线性布局LinearLayout,相对布局RelativeLayout,帧布局FrameLayout,表格布局TableLayout和绝对布局AbsoluteLayout。新型布局也就是约束布局ConstraintLayout,不过,在这篇文章我就想着重讲传统的五大布局,约束布局可见郭霖大神的文章(https://blog.csdn.net/guolin_blog/article/details/53122387)。
1.线性布局LinearLayout:
线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,每一个元素都位于前面一个元素之后。线性布局分为两种:水平方向和垂直方向的布局。分别通过属性android:orientation="vertical" 和 android:orientation="horizontal"来设置。
1.垂直方向

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="线性布局1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="线性布局1" />
    </LinearLayout>

2.水平方向

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="线性布局1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:textSize="18sp"
            android:text="线性布局1" />
    </LinearLayout>

因为很简单,就不挂图了。
3.LinearLayout还有一个特别特殊的属性就是layout_weight,我们可以把它理解为在水平或者垂直方向上控件均分多少

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:text="线性布局1" />

        <TextView
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:textSize="18sp"
            android:text="线性布局1" />
    </LinearLayout>

如上述代码就会使得第一个TextView占三分之一的宽度,第二个占三分之二的宽度
注意:在使用layout_weight属性的时候要注意是水平均分还是垂直均分,水平均分则将layout_width设置为0dp或者match_parent(这两种情况占比刚好相反,可以自己试一下),垂直均分则将layout_height设置为0dp或者match_parent。
2.相对布局RelativeLayout
RelativeLayout继承于android.widget.ViewGroup,其按照子元素之间的位置关系完成布局的,作为Android系统五大布局中最灵活也是最常用的一种布局方式,非常适合于一些比较复杂的界面设计。
我们先看一下RelativeLayout之所以这么强大的功能源于其强大的相对性,如下图所示:

image.png

通过以上的属性我们可以放置两个控件的相对位置。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.linkbasic.retrofittest.MainActivity">

    <Button
        android:id="@+id/bt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="居中" />

    <Button
        android:id="@+id/bt_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/bt_1"
        android:layout_toLeftOf="@id/bt_1"
        android:text="左侧" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/bt_1"
        android:layout_toRightOf="@id/bt_1"
        android:text="右侧" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/bt_2"
        android:layout_alignLeft="@id/bt_1"
        android:text="上方" />
</RelativeLayout>

上述布局如下图:

image.png

注意:在引用其他子元素之前,引用的ID必须已经存在,否则将出现异常。
3.帧布局FrameLayout
将所有的子元素放在整个界面的左上角,后面的子元素直接覆盖前面的子元素。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.linkbasic.retrofittest.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#f00" />

    <TextView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:background="#0f0" />

    <TextView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="#00f" />
</FrameLayout>

上述布局如下图:

image.png

4.表格布局和绝对布局
相信我,这两种几乎不用,所以不讲了。
5.共同的属性的含义
-margin
-padding
-layout_gravity
-gravity
image.png

三.shape样式的使用

shape是用于设置控件背景用的,比如给控件添加背景色,添加描边,添加渐变色,添加圆角。同时,shape本身可以绘制成矩形,椭圆,环形等样式。
1.我们在drawable文件夹下方新建一个shape.xml文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="8dp" />
    <gradient
        android:centerColor="#0f0"
        android:endColor="#00f"
        android:startColor="#f00" />
    <stroke
        android:width="5dp"
        android:color="#000" />
</shape>

然后在布局文件给Textview控件添加背景android:background="@drawable/shape"就会出现:


image.png

其中:

  • corners:表示圆角,可以单独定义左上,右上,左下,右下四个角的圆角大小
  • gradient:表示渐变色,还可以定义角度angle,45的倍数,让整个渐变色改变一定的角度
  • stroke:表示描边,可以定义描边的宽度,颜色,虚实线等。width定义实线的宽度,dashWidth和dashGap定义虚线宽度和间隔,color定义描边的颜色

四.选择器的使用

选择器就是让空间在不同的状态使用不同的shape样式,单独一个shape只有一种状态,选择器Selector可以在不同状态表现不同选择器,常用的状态有:
android:state_pressed:按下状态
android:state_selected:选中状态
android:state_checked:被checked了,如:一个RadioButton可以被check了
同样在drawable文件夹下方新建一个selector.xml文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/shape"/>
    <item android:state_pressed="false" android:drawable="@drawable/shape2"/>
</selector>

同时,再新建一个shape2:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="8dp" />
    <gradient
        android:centerColor="#0f0"
        android:endColor="#00f"
        android:startColor="#f00" />
</shape>

然后运行就会发现,未按下状态的时候没有描边,按下就有了描边。自己动手试试。

五.总结

以上就是关于布局和控件的知识点,如有不足或者错误的地方请指正。不管怎样,代码不只是需要多看,更需要通过自己动手去写去熟悉才能有更深的印象,更好更全面的了解。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容