美文网首页具体自定义控件程序员
Android_组合方式自定义控件

Android_组合方式自定义控件

作者: 书虫大王X | 来源:发表于2019-10-15 21:44 被阅读0次

用组合的方式自定义一个导航栏,功能:
左边个back被点击时,用监听的方式告诉系统自己被点击了了;右边个back被点击时,进入下一个界面。

a.png
在MainActivity中用代码实现:
定义一个xml资源文件,定义button控件的背景颜色和形状
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp"/>
    <solid android:color="#ffcccccc"/>
</shape>

定义一个wll类,继承于RelativeLayout ,用于定义俩个button的属性

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import androidx.core.content.res.ResourcesCompat;

public class wll extends RelativeLayout {
//  将两个控件全局化、私有化
    private Button back_button;
    private Button back_button1;
//  定义控件监听者
    private backButtonDidClickedListener listener;
//  实现RelativeLayout 的两个必须实现的方法
    public wll(Context context) {
        this(context,null);
    }
    public wll(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }
    public void init(Context context, AttributeSet attrs){
        setBackgroundColor(Color.MAGENTA);
        setBack_button(back_button);
        setBack_button1(back_button1);
    }
//  实现button的set、get方法
    public void setBack_button(Button back_button) {
        this.back_button = back_button;
//  创建控件
        back_button = new Button(getContext());
//  创建button的布局
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.leftMargin = (int)(10 * getResources().getDisplayMetrics().density);
        params.topMargin = (int)(10 * getResources().getDisplayMetrics().density);
//  取出之前定义的xml资源文件,用于设置button的背景
        Drawable drawable1 = ResourcesCompat.getDrawable(getResources(), R.drawable.back_shape, null);
        back_button.setBackground(drawable1);

        back_button.setText("Back");
//  给button添加监听事件
        back_button.setOnClickListener(new OnClickListener() {
//  给button添加点击事件
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.ButtonDidClicked();
                }
            }
        });
//  添加控件
        addView(back_button,params);
    }

    public void setBack_button1(Button back_button1) {
        this.back_button1 = back_button1;
        back_button1 = new Button(getContext());
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.leftMargin = (int)(300 * getResources().getDisplayMetrics().density);
        params.topMargin = (int)(10 * getResources().getDisplayMetrics().density);
        Drawable drawable1 = ResourcesCompat.getDrawable(getResources(), R.drawable.back_shape, null);
        back_button1.setBackground(drawable1);

        back_button1.setText("Back");
        back_button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.changeMainActivity();
                }
            }
        });
        addView(back_button1,params);
    }
    public Button getBack_button() {
        return back_button;
    }
    public Button getBack_button1() {
        return back_button;
    }
//  定义接口
    public interface backButtonDidClickedListener{
        void ButtonDidClicked();
        void changeMainActivity();
    }
//  添加监听者
    public void addbackButtonDidClickedListener(backButtonDidClickedListener listener){
        this.listener = listener;
    }
}

在MainActivity中调用wll类实现具体的功能

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//     定义一个wll类的对象
        wll yk = new wll(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,200);
//     实现接口定义的两个方法
        yk.addbackButtonDidClickedListener(new wll.backButtonDidClickedListener() {
            @Override
            public void ButtonDidClicked() {
                System.out.println("被点击了");
            }
            @Override
            public void changeMainActivity() {
                goToNext();
            }
        });
        setContentView(yk,params);
    }

//    跳转界面
    public void goToNext(){
//        创建一个intent,确定跳转的界面
//        intent:意图
//        显式意图(调到自己的界面)、隐式意图(调到别人的界面中)我定义的第二个界面命名为:Main2Activity
        Intent intent = new Intent(this, Main2Activity.class);

//        跳转
        startActivity(intent);
    }
}

使用xml的方式实现:
首先展示效果图:

展示Back
不展示Back
创建一个wll类,继承于LinearLayout
public class wll extends LinearLayout { 
//  全局化三个button
    private Button back1;
    private Button back2;
    private Button back3;
    private boolean isShowBack = false;
    int state = 0;
//  继承LinearLayout 中的方法
    public wll(Context context) {
        this(context,null);
    }
    public wll(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }
    private void init(Context context, AttributeSet attrs) {
        setOrientation(LinearLayout.HORIZONTAL);
        setBackgroundColor(Color.GRAY);
        boolean isShow1 = false;
//  获取自定义的属性
        if (attrs != null) {
            TypedArray array =context.obtainStyledAttributes(attrs,R.styleable.wll1);
            isShow1 = array.getBoolean(R.styleable.wll1_isShow_back,true);
        }
//  调用button控件的set方法,并将自定义的属性传递给控件(只有back1使用了自定义的属性)
        setBack1(back1,isShow1);
        setBack2(back2);
        setBack3(back3);
    }
//  实现控件的set、get方法
    public void setBack1(Button back1,boolean isShow) {
        this.back1 = back1;
//  自定义的属性如果为true,显示控件back1,否则不显示
        if (isShow == true) {
//  调用自定义的创建控件的方法
            initButton(back1,"Back",5);
        }
    }
    public void setBack2(Button back2) {
        this.back2 = back2;
//  调用自定义的创建控件的方法
        initButton(back2,"主页",65);
    }
    public void setBack3(Button back3) {
        this.back3 = back3;
//  调用自定义的创建控件的方法
        initButton(back3,"跳转",65);
    }
   public Button getBack1() {
        return back1;
    }
    public Button getBack2() {
        return back2;
    }
    public Button getBack3() {
        return back3;
    }
//  封装创建控件的方法,需要参数:button、button的标题、button之间的间距
    public void initButton(Button button,String text,int margin){
        button = new Button(getContext());
        button.setText(text);
        LinearLayout.LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.leftMargin = (int)(margin * getResources().getDisplayMetrics().density);
        addView(button,params);
    }

创建自定义属性的xml文件:

<resources>
    <declare-styleable name="wll1">
        <!--唯一的属性:back1是否显示-->
        <attr name="isShow_back" format="boolean"/>
    </declare-styleable>
</resources>

在MainActivity_xml中调用wll类:

<xn.ky.a1017_zidingyikongjian_lianxi_1.wll
       android:layout_width="match_parent"
       android:layout_height="100dp"
       app:isShow_back = "false"
       />

相关文章

  • Android_开发_Day28_自定义控件

    Android_开发Day28自定义控件 目的: 学会组合一些系统控件成一个控件,以满足某些需要 技术: <1> ...

  • Android_组合方式自定义控件

    用组合的方式自定义一个导航栏,功能:左边个back被点击时,用监听的方式告诉系统自己被点击了了;右边个back被点...

  • Android_开发_Day29_自己绘制控件

    Android_开发Day29自己绘制控件 目的: 在Android中很多时候系统的控件是不能满足需要的,组合方式...

  • 【Android】自定义控件

    Android自定义控件可以有三种实现方式 组合原生控件自己绘制控件继承原生控件 1 组合原生控件 1.1 组合原...

  • android自定义View基础

    自定义View基础1.1 分类自定义View的实现方式有以下几种 类型 定义自定义组合控件 多个控件组合成为一个...

  • flutter 自定义帐号输入控件

    自定义组合控件 LoginInputText代码如下: 使用方式如下 使用TextEditingControlle...

  • 自定义view

    一、自定义view实现方式 二、组合控件 将系统原有的控件进行组合,构成一个新的控件。 定义标题栏的布局文件cus...

  • 实现高亮某行的RecyclerView效果

    全部代码: 方式有二 组合控件,RecyclerView + View 自定义RecyclerView 高亮的动画...

  • Android入门06 -- 自定义控件

    自定义组合控件 将几个子控件组合在一起,形成一个可复用的新的组合控件,自定义组合控件一般继承自RelativeLa...

  • android2019-01-03

    1.View的绘制流程自定义控件:1、组合控件。这种自定义控件不需要我们自己绘制,而是使用原生控件组合成的新控件。...

网友评论

    本文标题:Android_组合方式自定义控件

    本文链接:https://www.haomeiwen.com/subject/oqdemctx.html