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

    拼凑自定义控件

    引用布局的技巧确实解决了重复编写布局代码的问题, 但还有优化的空间.
    比如说标题栏中的返回按钮,其实不管是在哪一个活动中,这个按钮的功能都是相同的,即销毁当前Activity。
    这种情况最好是使用自定义控件的方式来解决。

    1 新建 TitleLayout 继承自 LinearLayout,让它成为我们自定义的标题栏控件

    代码如下所示:

    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;
    
    public class  extends LinearLayout {
    
        public (Context context, AttributeSet attrs) {
            super(context, attrs);
    
            LayoutInflater.from(context).inflate(R.layout.title, this);
            Button titleBack = (Button) findViewById(R.id.title_back);
            Button titleEdit = (Button) findViewById(R.id.title_edit);
    
            titleBack.setOnClickListener(new OnClickListener() {
                
                public void onClick(View v) {
                    
                    ((Activity) getContext()).finish();
                }
            });
    
            titleEdit.setOnClickListener(new OnClickListener() {
                
                public void onClick(View v) {
                    // 吐司
                    Toast.makeText(getContext(), "你点击了右侧按钮", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    

    tiele布局文件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_banner" >
    
        <Button
            android:id="@+id/title_back"
            an 大专栏  拼凑自定义控件droid:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dip"
            android:background="@drawable/bt_menu_show"
            android:text="Back"
            android:textColor="#fff" />
    
        <TextView
            android:id="@+id/title_text"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Title Text"
            android:textColor="#fff"
            android:textSize="24sp" />
    
        <Button
            android:id="@+id/title_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="5dip"
            android:background="@drawable/ico_letter"
            android:text="Edit"
            android:textColor="#fff" />
    
    </LinearLayout>
    

    2 在Activity的布局中添加这个自定义控件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <com.yassblog.TitleLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </com.yassblog.TitleLayout>
    
    </LinearLayout>   
    

    3 这样的话,每当我们在一个页面的布局中引入 TitleLayout,左侧返回按钮和右侧编辑按钮的点击事件就已经自动实现好了,也是省去了很多编写重复代码的工作。

    4 如果在某个页面,这两个按钮有不同的功能,可通过引用布局的方式实现

  • 相关阅读:
    2020年9月29日
    随机生成验证码
    动手动脑java语法基础
    Java语法之动手实验
    代码大全2 读书笔记
    java动手动动脑之字串联接
    二柱子问题
    生成随机四则运算1
    可变参数
    2020年9月30日
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12147462.html
Copyright © 2011-2022 走看看