美文网首页Android开发
自定义View之组合View

自定义View之组合View

作者: 飞马_6886 | 来源:发表于2019-07-03 11:11 被阅读0次

前言

自定义View是安卓开发中比较重要的一环,很多地方都需要用到自定义View。而自定义View比较常见的一种形式就是组合View,也是比较简单的一种方式。下面通过一个实例来学习一下自定义组合view。
先来一张效果图吧。

自定义TitleBar

没错,今天就一起来实现一下自定义TitleBar,自定义属性一般是少不了的,先看一下自定义属性文件代码,在value文件夹下,文件名一般为attr.xml 或 attrs.xml,这里我们定义了三个属性:布局的背景色、中间的文本及颜色。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CusTitleBar">
        <attr name="bg_color" format="color"></attr>
        <attr name="text_color" format="color"></attr>
        <attr name="title_text" format="string"></attr>
    </declare-styleable>
</resources>

接下来就是MyTitleBar的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:id="@+id/layout_titlebar_root"
    >
    <ImageView
        android:id="@+id/btn_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@mipmap/ico_return"
        android:paddingLeft="10dp"
         />

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textSize="16sp"
        android:text="" />

    <ImageView
        android:id="@+id/btn_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@mipmap/ico_title_right"
        android:paddingRight="10dp"
         />
</RelativeLayout>

然后就是然后就是最重要的MyTitleBar.java 文件

public class MyTitleBar extends RelativeLayout {

    private int mBgColor = Color.BLUE;
    private int mTextColor = Color.WHITE;
    private String mTitleText = "";

    private ImageView btn_left;
    private ImageView btn_right;
    private TextView tvTitle;
    private RelativeLayout relativeLayout;

    public MyTitleBar(Context context) {
        super(context);
        initView(context);
    }

    public MyTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initTypeValue(context,attrs);
        initView(context);
    }

    public MyTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTypeValue(context,attrs);
        initView(context);
    }

    public void  initTypeValue(Context context ,AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CusTitleBar);
        mBgColor = a.getColor(R.styleable.CusTitleBar_bg_color, Color.YELLOW);
        mTitleText = a.getString(R.styleable.CusTitleBar_title_text);
        mTextColor = a.getColor(R.styleable.CusTitleBar_text_color,Color.RED);
        a.recycle();
    }

    public void initView(Context context){
        LayoutInflater.from(context).inflate(R.layout.layout_custom_titlebar,this,true);

        btn_left = findViewById(R.id.btn_left);
        btn_right = findViewById(R.id.btn_right);
        tvTitle = findViewById(R.id.tv_title);
        relativeLayout = findViewById(R.id.layout_titlebar_root);

        relativeLayout.setBackgroundColor(mBgColor);
        tvTitle.setTextColor(mTextColor);
        tvTitle.setText(mTitleText);
    }

    public void setLeftClickListener(OnClickListener listener){
        btn_left.setOnClickListener(listener);
    }

    public void setRightClickListener(OnClickListener listener){
        btn_right.setOnClickListener(listener);
    }

    public void setTitleText(String str){
        if(!TextUtils.isEmpty(str)){
            tvTitle.setText(str);
        }
    }
}

通过以上步骤,我们的自定义View就做好了,下面就看一下怎么调用吧

<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"
    android:background="#eee"
    tools:context=".MainActivity">


    <com.ma.actionbar.MyTitleBar
        xmlns:apps="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_custom_action_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#aaa"
        apps:title_text="标题"
        apps:text_color = "#fff"
        apps:bg_color = "#b3f"
        />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
    
    private MyTitleBar titleBar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    public void init(){
        titleBar = findViewById(R.id.my_custom_action_bar);
        titleBar.setTitleText("我的标题");
        titleBar.setLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"左点击",Toast.LENGTH_SHORT).show();
            }
        });

        titleBar.setRightClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"右点击",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

结语

以上就是自定义MyTitleBar的完整过程。在我们的实际项目中使用,可以简洁我们的代码,提升我们的开发效率。

相关文章

网友评论

    本文标题:自定义View之组合View

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