自定义View和自定义属性

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Jonly_W/article/details/79952312

   

  首先在res 的valuse文件包里新建  attrs文件

代码如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Myviewname">
        <attr name="left_text_color" format="color"></attr>
        <attr name="right_text_color" format="color"></attr>
        <attr name="backgroud_color" format="color"></attr>
    </declare-styleable>
</resources>

然后在新建类 继承LinearLayout

package com.bawei.myview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by  on 2018/4/15.
 */

public class Myview extends LinearLayout implements View.OnClickListener, View.OnLongClickListener {

     private int s_str;
    private   TextView show;

    public Myview(Context context) {
        this(context,null);
    }

    public Myview(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public Myview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        intview(context, attrs, defStyleAttr);
    }

    private void intview(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.Myviewname);
        int left_color = array.getColor(R.styleable.Myviewname_left_text_color, Color.GRAY);
        int right_color = array.getColor(R.styleable.Myviewname_right_text_color, Color.GRAY);
        int back_color = array.getColor(R.styleable.Myviewname_backgroud_color, Color.GRAY);




        View view = View.inflate(context, R.layout.item, this);
        Button btn1 = view.findViewById(R.id.btn1);
        Button btn2=view.findViewById(R.id.btn2);
       show=view.findViewById(R.id.show);
        //按钮设置自定义属性
        btn1.setTextColor(left_color);
        btn2.setTextColor(right_color);
        show.setBackgroundColor(back_color);
        //获取显示的数值
        String s = show.getText().toString();
        s_str=Integer.valueOf(s);
        //按钮的监听
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn2.setOnLongClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn1:
               if(s_str>1){
                   s_str--;
                   show.setText(s_str+"");
                   Toast.makeText(getContext(),"你点击了减号",Toast.LENGTH_SHORT).show();
               }else {
                   Toast.makeText(getContext(),"数量不能小于1",Toast.LENGTH_SHORT).show();
               }

                break;
            case R.id.btn2:
                s_str++;
                show.setText(s_str+"");
                Toast.makeText(getContext(),"你点击加号",Toast.LENGTH_SHORT).show();
                break;
        }

    }

    @Override
    public boolean onLongClick(View view) {
        s_str+=s_str;
        show.setText(s_str+"");
        Toast.makeText(getContext(),"长按按钮",Toast.LENGTH_SHORT).show();
        return true;
    }
}

还有 自定义布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="减"
        android:id="@+id/btn1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/show"
        android:text="0"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="加"
        android:id="@+id/btn2"/>
</LinearLayout>

全部写完后  在主布局引用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.bawei.myview.MainActivity">

    <com.bawei.myview.Myview
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:left_text_color="#f00"
        app:right_text_color="#626"
        app:backgroud_color="#f51">

    </com.bawei.myview.Myview>
</LinearLayout>
 

阅读更多

没有更多推荐了,返回首页