zoukankan      html  css  js  c++  java
  • Android 自定义控件----基础

    先来看下布局和控件的继承结构

    可以看到,所有的控件都是继承或者间接继承View。所用到的布局也是直接或者间接继承View,View是最基本的一种UI组件,可以绘制一个矩形区域,并且能够响应这块区域各种事件。

    在Layout中国新建一个title,xml的布局,代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#F50313"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/titleBack"
            android:layout_gravity="center"
            android:text="返回"
            android:textColor="#fff"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="标题"
            android:layout_weight="1"
            android:textColor="#fff"
            android:textSize="24sp"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dp"
            android:text="编辑"
            android:textColor="#FFF"/>
    </LinearLayout>

    反正没人看,丑就丑吧

    然后将activity_main.xml代码改写为:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
        <include layout="@layout/title"/>
    </LinearLayout>

    并在MainActivity中加入一行代码,将原本的标题栏隐藏

    supportActionBar?.hide()

     一个标题两个按钮就出来了。

    但是这不算自定义控件,因为我们现在无法对按钮的点击事件作出反应。

    新建一个TitleLayout继承LinearLayout。让它成为我们的自定义标题栏控件。并且定义了两个按钮的点击事件回调,代码如下:

    import android.app.Activity
    import android.content.Context
    import android.util.AttributeSet
    import android.view.LayoutInflater
    import android.widget.LinearLayout
    import android.widget.Toast
    import kotlinx.android.synthetic.main.title.view.*

    class TitleLayout(context: Context, attrs:AttributeSet) :LinearLayout(context,attrs){
    init {
    LayoutInflater.from(context).inflate(R.layout.title,this)
    titleBack.setOnClickListener {
    val activity=context as Activity
    activity.finish()
    }
    titleEdit.setOnClickListener {
    Toast.makeText(context,"你点击了编辑",Toast.LENGTH_LONG).show()
    }
    }
    }

    现在自定义控件已经创建好了,接下来就可以在布局文件中添加了。修改activty_mai的代码。

        <com.ljx.listviewtest.TitleLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

     运行结果如上。先这样,睡觉

  • 相关阅读:
    什么叫大数据,与云计算有何关系?
    基于TI 多核DSP 的大数据智能计算处理解决方案
    加固智能计算异构服务器
    Kintex7 XC7K325T 板卡五兄弟
    英伟达GPU 嵌入式开发平台
    NVIDIA Jetson™ TX1 Module
    Linux 高性能服务器编程——多线程编程
    Linux 高性能服务器编程——多进程编程
    Linux 高性能服务器编程——I/O复用的高级应用
    Linux 高性能服务器编程——I/O复用
  • 原文地址:https://www.cnblogs.com/inthecloud/p/13228011.html
Copyright © 2011-2022 走看看