安卓开发详解布局

布局是一种可以放置多个控件或者布局的容器,可以按照一定的规律调整内部控件或布局的位置,从而来写出精美的界面。
在这里插入图片描述
● 所有控件都是直接或间接继承自View
● 所有的布局都是直接或间接继承自ViewGroup
● View是Android中最基本的一种UI控件,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件
● ViewGroup是一种特殊的View,它可以包含很多子View和子ViewGroup,是一个

线性布局

LinearLayout被叫为线性布局,该布局会将它所包含的控件在线性方向(水平或垂直方向)上依次排。
android:orientation="vertical"指定为垂直方向的排序
android:orientation="horizontal"指定为水平方向的排序

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="Button 1"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Button 2"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Button 3"/>

	<EditText
        android:id="@+id/input_message"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Type something"/>

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send"/>

</LinearLayout>

相对布局

RelativeLayout相对布局,通过定位的方式让控件出现在布局的任何位置。

帧布局

FrameLayout帧布局,所有的控件默认的放在布局的左上角

百分比布局

FrameLayout不再用wrap_cotent,match_parent.等方法来实现指定控件的大小。LinearLayout本身支持按比例指定控件大小,所以百分比布局是RelativeLayout和FrameLayout的扩展。
百分比布局是新增的布局需要添加库依赖。
————————————————————————————————————————

引用布局

例如:创建一个标题栏布局并把它引入主布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/title_bg">

    <Button
        android:id="@+id/title_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/back_bg"
        android:text="Back"
        android:textColor="#fff"/>

    <TextView
        android:id="@+id/title_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Title Text"
        android:textColor="#fff"
        android:textSize="24sp"/>

    <Button
        android:id="@+id/title_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@drawable/edit_bg"
        android:text="Edit"
        android:textColor="#fff"/>

</LinearLayout>

android:background 用于给控件或布局指定一个背景
android:layout_margin 指定控件在上下左右方向上偏移的距离,单位为dp

引入自定的标题布局
<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <include layout="@layout/title"/>

</LinearLayout>

使用include将标题布局引入进去
然后把MainActivity中自带的标题栏隐藏。

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.hide();
}

自定义控件

引入布局很好的解决了重复编写布局代码的问题,但是如果布局中有一些控件要求能够响应事件,那么我们就需要在每个活动中单独编写一次事件注册的代码。比如标题栏中的返回按钮,其实不管在哪个活动中,这个按钮的功能都是销毁当前活动。如果每一个活动中都需要重新注册一遍返回按钮的点击事件,这样就会增加很多重复代码,此时我们就应使用自定义控件的方式来解决。
新建TitleLayout继承LinearLayout

public class TitleLayout extends LinearLayout
{
	public TitleLayout(Content context,AttributeSet attrs)
	{
		super(context,attrs);
		LayoutInflater.from(context).inflate(R.layout.title,this);
	 button = view.findViewById(R.id.button);
        button1 = view.findViewById(R.id.button1);
        button.setOnClickListener(this);
        button1.setOnClickListener(this);
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                Toast.makeText(getContext(),"button1",Toast.LENGTH_LONG).show();
                break;
            case R.id.button1:
                Toast.makeText(getContext(),"button2",Toast.LENGTH_SHORT).show();
                break;
        }
    }
}

Layout inflation是在android系统中使用的术语,当XML布局资源被解析并转换成View对象时会用到。
● 重写了LinearLayout带有两个参数的构造函数,在布局中引入TitleLayout控件就会调用这个构造函数
● 使用LayoutInflater对标题栏布局进行动态加载,LayoutInflater的from()方法构建出一个LayoutInflater对象,然后调用inflate()方法动态加载一个布局。
inflate()方法第一个参数是要加载布局文件的id,第二个参数是给加载好的布局添加一个父布局。
● 通过findViewById()找到布局文件中的控件,分别为各个按钮注册点击事件。

自定义控件已经创建完成 然后我们需要在布局文件中添加这个自定义控件,修改activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent“
    <com.example.uicustomviews.Title
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    />
</LinearLayout>

以后我们在布局中引入TitleLayout后,点击事件也会编译好。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值