1. 程式人生 > >自定義titilbar控制元件

自定義titilbar控制元件

自定義titilbar控制元件

自定義佈局

<?xml version="1.0" encoding="utf-8"?>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="30dp"
    android:layout_height="30dp"
    tools:srcCompat="@tools:sample/avatars" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="center"
    android:text="TextView" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="30dp"
    android:layout_height="30dp"
    tools:srcCompat="@tools:sample/avatars" />

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**

  • date:2018/11/1 11:21

  • author:

  • fileName:TitleBar
    */
    public class TitleBar extends LinearLayout {

    private LinearLayout linearLayout;
    private ImageView left,right;
    private TextView text;

    public TitleBar(Context context) {
    //相互呼叫
    this(context,null);
    }

    public TitleBar(Context context, AttributeSet attrs) {
    //相互呼叫
    this(context, attrs,-1);
    }

    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context);
    initAttrs(context,attrs);
    initIn();
    }
    private void init(Context context){
    linearLayout = (LinearLayout) View.inflate(context,R.layout.title,this);
    left = linearLayout.findViewById(R.id.imageView1);
    text = linearLayout.findViewById(R.id.textView);
    right = linearLayout.findViewById(R.id.imageView2);
    }
    private void initAttrs(Context context, AttributeSet attrs){
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
    Drawable drawable = typedArray.getDrawable(R.styleable.TitleBar_imageView1);
    if(drawable!=null){
    left.setImageDrawable(drawable);
    }
    CharSequence text1 = typedArray.getText(R.styleable.TitleBar_textVie);
    if(!TextUtils.isEmpty(text1)){
    text.setText(text1);
    }
    int color = typedArray.getColor(R.styleable.TitleBar_textVie_color, -1);
    if(color!=-1){
    text.setTextColor(color);
    }
    float dimension = typedArray.getDimension(R.styleable.TitleBar_textVie_size, 0f);
    text.setTextSize(dimension);
    Drawable drawable1 = typedArray.getDrawable(R.styleable.TitleBar_imageView2);
    if(drawable1!=null){
    right.setImageDrawable(drawable1);
    }
    }
    private void initIn(){
    left.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    if(getLeft!=null){
    getLeft.ongetLeft(v);
    }
    }
    });
    right.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    if(getRight!=null){
    getRight.ongetRight(v);
    }
    }
    });
    }

private getData getLeft;
private getData getRight;

public void setData(getData getLeft,getData getRight){
this.getLeft = getLeft;
this.getRight = getRight;
}

public interface getData{
void ongetLeft(View view);
void ongetRight(View view);
}
}

在values下建立xml為attrs

<?xml version="1.0" encoding="utf-8"?>

主介面xml引入佈局

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android=“http://schemas.android.com/apk/res/android
xmlns:app=“http://schemas.android.com/apk/res-auto
xmlns:tools=“http://schemas.android.com/tools
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">

<com.bw.qgs.titlebar2.TitleBar
    app:imageView1="@drawable/left"
    app:textVie="啥地方絕對是"
    app:textVie_color="#31ded5"
    app:textVie_size="20sp"
    app:imageView2="@drawable/right"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />

</android.support.constraint.ConstraintLayout>

主介面MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private TitleBar title;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    title = findViewById(R.id.title);
    title.setData(new TitleBar.getData() {
        @Override
        public void ongetLeft(View view) {
            MainActivity.this.finish();
        }

        @Override
        public void ongetRight(View view) {

        }
    }, new TitleBar.getData() {
        @Override
        public void ongetLeft(View view) {

        }

        @Override
        public void ongetRight(View view) {
            Toast.makeText(MainActivity.this, "點選了右邊", Toast.LENGTH_SHORT).show();
        }
    });
}

}