Android 自定义控件

Android 自定义控件

本文参考书籍:郭霖《第一行代码-Android》。

关键字:自定义控件

需求

对于多个Activity,在界面顶部使用一个统一的标题栏,标题栏上:返回按钮-标题文字-编辑按钮,点击可响应操作

简单分析

把这个标题栏做成一个控件,在每个Activity中使用

  • xml定义标题栏样式
  • 创建自己的控件类class
  • 在主Activity的xml文件中使用自定义的控件

XML和代码文件

  • title_all.xml, 定义标题栏样式
<?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="match_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/title_btn_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="back" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:text="this is the title" />

    <Button
        android:id="@+id/title_btn_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="edit" />

</LinearLayout>
  • MyTitleLayout.java, 创建自己的控件类,包含样式和事件响应:
package com.example.androidcustomedviewtest;

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;

import com.myising99.androidcustomedview.R;

public class MyTitleLayout extends LinearLayout
{
    public MyTitleLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title_all, this); // 填充布局

        Button btnBack = (Button) findViewById(R.id.title_btn_back);
        Button btnEdit = (Button) findViewById(R.id.title_btn_edit);

        // 注册事件
        btnBack.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                ((Activity) getContext()).finish();
            }
        });

        btnEdit.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Toast.makeText(getContext(), "clicked edit btn", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
  • activity_main.xml, 在主Activity的xml文件中使用自定义控件:
<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"
    tools:context="com.example.androidcustomedviewtest.MainActivity" >

    <!-- 自定义控件 com.example.androidcustomedview.MyTitleLayout, 需要指明控件的完整名 -->

    <com.example.androidcustomedviewtest.MyTitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </com.example.androidcustomedviewtest.MyTitleLayout>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/hello_world" />

</LinearLayout>
  • MainActivity.java, 主Activity:
package com.example.androidcustomedviewtest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;

import com.myising99.androidcustomedview.R;

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); // 先去掉系统默认title bar
        setContentView(R.layout.activity_main);
    }
}

当主Activity运行时,界面顶部就显示自定义的控件。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值