zoukankan      html  css  js  c++  java
  • 从0系统学Android--3.2四种基本布局

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


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

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

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

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

    可以看到说有的控件都是直接或者间接继承 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) {
                    // 你自己想做的事情
                }
            });
        }
    }
    

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

  • 相关阅读:
    hibernate_0100_HelloWorld
    MYSQL子查询的五种形式
    JSF是什么?它与Struts是什么关系?
    nop指令的作用
    htmlparser实现从网页上抓取数据(收集)
    The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the
    FCKeditor 在JSP上的完全安装
    Java遍历文件夹的2种方法
    充电电池和充电时间说明
    吃知了有什么好处
  • 原文地址:https://www.cnblogs.com/sydmobile/p/11979726.html
Copyright © 2011-2022 走看看