码迷,mamicode.com
首页 > 移动开发 > 详细

Android自定义控件

时间:2015-12-10 21:53:04      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

先在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为自定义控件
 

Android自定义控件

标签:

原文地址:http://www.cnblogs.com/flypie/p/5037205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!