• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小飞侠
find beauty of life
博客园    首页    新随笔    联系   管理    订阅  订阅
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为自定义控件
 
posted on 2015-12-10 20:24  flypie  阅读(283)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3