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

    先在layout中新建一个自定义控件的布局文件(一般为LinearLayout布局)
     
    代码如下:
    <?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="50dp"
        android:background="@mipmap/icon_title">

        <Button
            android:id="@+id/btnUser"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@mipmap/icon_body" />

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="首页"
            android:textSize="25sp" />

        <Button
            android:id="@+id/btnConfig"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@mipmap/icon_left" />

    </LinearLayout>
     
    如图:
     
     
    然后新建控件的类,该类继承自LInearLayout
     
    代码如下:
     
    package com.example.flypie.notesbook.Layout;

    import android.content.Context;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.example.flypie.notesbook.R;

    /**
     * Created by FLYPIE on 2015/12/9.
     */
    public class TitleLayout extends LinearLayout implements View.OnClickListener{

        Button btnUser;
        Button btnConfig;
        TextView tvTitle;

        public TitleLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
            LayoutInflater.from(context).inflate(R.layout.menu_title, this);
            initview();
            setlistener();
        }


        private void setlistener() {
            btnUser.setOnClickListener(this);
            btnConfig.setOnClickListener(this);
        }

        private void initview() {
            btnUser= (Button) findViewById(R.id.btnUser);
            btnConfig= (Button) findViewById(R.id.btnConfig);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btnUser:
                    Toast.makeText(getContext(), "btnUser", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btnConfig:
                    Toast.makeText(getContext(),"btnConfig",Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    }
     
    最后,只要在需要的地方引入该控件即可:
     
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <com.example.flypie.notesbook.Layout.TitleLayout
            android:id="@+id/zdyTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </com.example.flypie.notesbook.Layout.TitleLayout>

        <ListView
            android:id="@+id/lvNotes"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/zdyTitle">

        </ListView>
    </RelativeLayout>

     
    其中的com.example.flypie.notesbook.Layout.TitleLayout为自定义控件
     
  • 相关阅读:
    javascript中万恶的function
    Windows7下如何安装部署秋色园CYQBlog源码V1.0网站
    Extjs2.2:Panel里面嵌入Excel表格
    Extjs做界面很酷;感谢博客园给我一个展示的机会;借此向大家展示一下EXTJS的魅力
    16Aspx.com改进版Extjs简单版酒店管理系统提供下载!
    Ext2.2+ASP.NET开发框架已完成欢迎大家下载!
    Extj+Asp.net开发框架V1.1树的操作
    Ext2.2程序开发实战(1)登录界面
    扩展欧几里得定理
    C语言 统计整数二进制表示中1的个数
  • 原文地址:https://www.cnblogs.com/flypie/p/5037205.html
Copyright © 2011-2022 走看看