Android学习笔记--6.自定义控件的简单使用

本文介绍了如何在Android应用中创建并使用自定义控件。通过实例演示了如何制作带有响应事件的标题栏,并解决了实现过程中遇到的问题。

前言:在自定义控件的学习中遇到了两个小问题,第一个是隐藏标题栏的时候写了代码没有起到效果,后来发现Android Studio创建活动的时候继承的是AppCompatActivity,书上的requestWindowFeature这个其实是不适用的,应该使用这个supportRequestWindowFeature方法才可以。还有一个问题是,在引用自定义控件的时候,提示找不到包名,刷新一下后发现还是不可以,最后用真机直接运行发现,是可以显示的。然后再说说这个自定义控件,我的写的简单的例子是带有响应事件的标题栏,大家都知道标题栏是经常使用的,所以把标题栏写成了自定义控件。

1.创建一个CustomLayout布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="#f0f0">
<Button
    android:id="@+id/back_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#f0f0"
    android:textColor="#fff"
    android:layout_margin="5dp"
    android:text="返回"
    />
    <TextView
        android:id="@+id/title_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="主页"
        android:textColor="#fff"
        android:textSize="24sp"
        android:gravity="center"
        android:layout_weight="1"
        />
    <Button
        android:id="@+id/edit_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="#f0f0"
        android:text="编辑"
        android:textColor="#fff"
        android:layout_margin="5dp"/>
</LinearLayout>

2.创建TitleLayout类,继承于LinearLayout“

这里主要是重写TitleLayout的构造参数,使用LayoutInflater.from()方法构造一个对象,然后用inflate()方法添加刚才写的布局文件,第二个参数是指把布局添加到当前的类上,即这个类成为了父布局。

package com.example.administrator.uilayouttest;

import android.app.Activity;
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.Toast;

/**
 * Created by Administrator on 2016/9/5.
 */
public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context, AttributeSet attrs){
        super(context,attrs);
        LayoutInflater.from(context).inflate(R.layout.customlayout,this);

        Button mBackButton = (Button)findViewById(R.id.back_btn);
        Button mEditButton = (Button)findViewById(R.id.edit_button);
        mBackButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity)getContext()).finish();
            }
        });
        mEditButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "you click editBtn", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

3.在主布局文件中引入带有响应事件的自定义控件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical">
    <com.example.administrator.uilayouttest.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

4.主活动的代码

package com.example.administrator.uilayouttest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatPopupWindow;
import android.view.Window;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // requestWindowFeature();不支持AppCompatActivity
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值