从0系统学Android--3.2四种基本布局

作者: sydMobile | 来源:发表于2019-12-03 21:49 被阅读0次

从0系统学Android--3.2四种基本布局

image image
本系列文章目录更多精品文章分类

本系列持续更新中....

3.3 系统控件不够用?创建自定义控件

上一节我们学习了 Android 中的一些常用的控件和布局的用法。这里我们来看一下他们的关系图

image

可以看到说有的控件都是直接或者间接继承 View ,所有的布局都是直接或者间接继承 ViewGroup

View 是 Android 中最基本的一种 UI 组件,它可以在屏幕上绘制一块矩形区域,并且能够响应这块区域的各种事件,因此,我们使用的各种控件其实就是在 View 的基础的又添加了一些特有的功能。而 ViewGroup 是一种特殊的 View ,它可以包含很多子 View 和 子 ViewGroup,是一个用于放置控件和布局的容器。

那么当系统给我提供的控件不能满足我们的需要的时候,我们也可以自己创建符合我们自己需求的控件。

3.4.1 引入布局

我们知道现在的应用程序几乎在界面顶部都有一个标题栏,虽然 Android 系统已经给我们提供了,但是这里我们不用它,我们自己创建一个。

我们自己创建一个布局

<?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="wrap_content"
    android:background="@color/colorPrimary"
    android:orientation="horizontal">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title_back"
        android:background="@color/colorAccent"
        android:layout_gravity="center"
        android:text="back"
        android:textAllCaps="false"
        android:textColor="#FFFFFF"/>
    <TextView
        android:layout_gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:textSize="24sp"
        android:layout_height="wrap_content"
        android:text="Text Title"
        android:id="@+id/title_text"
        android:gravity="center"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="5dp"
        android:background="@color/colorPrimaryDark"
        android:text="Edit"
        android:textAllCaps="false"/>
</LinearLayout>

就这样这个简单的标题栏布局就写好了,那么如何使用呢?很简单,在需要使用的布局中。

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

就添加上面一句话就把刚刚的布局引入了。

使用的时候不要忘了隐藏自带的标题栏

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ui);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar !=null){
            actionBar.hide();
        }
        initView();

    }

3.4.2 创建自定义控件

引入布局的技巧确实解决了重复编写布局代码的问题,但是布局中有一些控件还需要响应事件,这种情况就需要我们来自定义控件了。

新建 TitleLayout 继承自 LinearLayout,让它作为我们自定义标题栏的控件。

public class TitleLayout extends LinearLayout {
    
    public TitleLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title,this);
        Button btBack = findViewById(R.id.title_back);
        Button btEdit = findViewById(R.id.bt_edit);
        btBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity)getContext()).finish();
            }
        });
        btEdit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // 你自己想做的事情
            }
        });
    }
}

好了这样一个标题栏自定义控件就完成了。

相关文章

  • 从0系统学Android--3.2四种基本布局

    从0系统学Android--3.2四种基本布局 本系列持续更新中.... 3.3 系统控件不够用?创建自定义控件 ...

  • 从0系统学Android--3.2四种基本布局

    从0系统学Android--3.2四种基本布局 本系列持续更新中.... 一个界面总是要由许多的控件来组成的,如何...

  • 从0系统学Android目录

    从 0 开始系统学 Android 系列 从0系统学 Android--1.1认识 Android

  • 四种基本布局

    一、LinearLayout LinearLayout 又称作线性布局,这个布局会将它所包含的控件在线性方向上依次...

  • Swift - 基本数据类型,及常/变量声明

    原文地址:从0到1,Swift系统学习路线 (本文代码已升级至Swift4) 下面是 Swift 中基本的数据类型...

  • CSS布局

    基本上,有四种可选的布局类型:1.固定宽度布局2.自适应布局3.弹性布局4.混合布局 1.固定布局以像素为单位,外...

  • 1.aOS_xml布局

    0.基本 1>.sp用作字体的size,dp用做宽高。 1.简单的代码布局 2.xml布局基本属性 1>.线性布局...

  • 《Android》Lesson11-UI布局

    Week07 2016/10/25上午1-4节 一、复习 二、无种基本布局 0、布局:布局,控件 Android...

  • bootstrap网格系统

    基本用法:网格系统用来布局,其实就是列的组合。Bootstrap框架的网格系统中有四种基本的用法。由于Bootst...

  • iOS布局原点 (0,0)与(0,64/88)

    从iOS 7以后ViewController布局起点发生改变,默认情况下从左上角即(0,0)点开始布局,即使屏幕中...

网友评论

    本文标题:从0系统学Android--3.2四种基本布局

    本文链接:https://www.haomeiwen.com/subject/onbhgctx.html