Android自定义View之自定义组合控件
Posted 「已注销」
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android自定义View之自定义组合控件相关的知识,希望对你有一定的参考价值。
运行效果
android自定义组合控件之titleBar
https://www.bilibili.com/video/BV17w411Z7pV/
代码
- values
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FlowLayout">
<attr name="android:gravity"/>
<attr name="android:horizontalSpacing" format="dimension |reference"/>
</declare-styleable>
<declare-styleable name="FlowLayout_Layout">
<attr name="android:layout_gravity"/>
</declare-styleable>
<declare-styleable name="TitleBar">
<attr name="title_text_color" format="color"/>
<attr name="title_bg" format="color"/>
<attr name="title_text" format="string"/>
</declare-styleable>
</resources>
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
</resources>
- Layout
activity_main3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.as.proj.myviewgroupdemo2.TitleBar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="45dp"
app:title_text="自定义组合控件"
app:title_bg="@android:color/holo_orange_dark"
app:title_text_color="@android:color/holo_blue_dark">
</com.example.as.proj.myviewgroupdemo2.TitleBar>
</LinearLayout>
view_costomtile.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_titlebar_rootlayout"
android:layout_width="fill_parent"
android:layout_height="45dp"
>
<ImageView
android:id="@+id/iv_titlebar_left"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:src="@drawable/ico_return"
/>
<TextView
android:id="@+id/tv_titlebar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:maxEms="11"
android:singleLine="true"
android:ellipsize="end"
android:textStyle="bold"/>
<ImageView
android:id="@+id/iv_titlebar_right"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:src="@drawable/title_right"
android:gravity="center"
android:padding="15dp"
/>
</RelativeLayout>
- Java
MainActivity3.java
package com.example.as.proj.myviewgroupdemo2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity3 extends AppCompatActivity
private TitleBar mTitleBar;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
mTitleBar = (TitleBar) this.findViewById(R.id.title);
mTitleBar.setLeftListener(new View.OnClickListener()
@Override
public void onClick(View v)
Toast.makeText(MainActivity3.this, "点击左键", Toast.LENGTH_SHORT).show();
);
mTitleBar.setRightListener(new View.OnClickListener()
@Override
public void onClick(View v)
Toast.makeText(MainActivity3.this, "点击右键", Toast.LENGTH_SHORT).show();
);
TitleBar.java
package com.example.as.proj.myviewgroupdemo2;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class TitleBar extends RelativeLayout
private ImageView iv_titlebar_left;
private ImageView getIv_titlebar_right;
private TextView tv_titlebar_title;
private RelativeLayout layout_titlebar_rootlayout;
private int mColor = Color.BLUE;
private int mTextColor = Color.WHITE;
private String titleName;
public TitleBar(Context context)
super(context);
initView(context);
public TitleBar(Context context, AttributeSet attrs)
super(context, attrs);
initTypedArray(context, attrs);
initView(context);
public TitleBar(Context context, AttributeSet attrs, int defStyleAttr)
super(context, attrs, defStyleAttr);
initTypedArray(context, attrs);
initView(context);
private void initTypedArray(Context context, AttributeSet attrs)
TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.TitleBar);
mColor = mTypedArray.getColor(R.styleable.TitleBar_title_bg, Color.BLUE);
mTextColor = mTypedArray.getColor(R.styleable.TitleBar_title_text_color, Color.WHITE);
titleName = mTypedArray.getString(R.styleable.TitleBar_title_text);
//获取资源后要及时回收
mTypedArray.recycle();
private void initView(Context context)
LayoutInflater.from(context).inflate(R.layout.view_customtile, this, true);
iv_titlebar_left = (ImageView)findViewById(R.id.iv_titlebar_left);
getIv_titlebar_right = (ImageView)findViewById(R.id.iv_titlebar_right);
tv_titlebar_title = (TextView)findViewById(R.id.tv_titlebar_title);
layout_titlebar_rootlayout = (RelativeLayout)findViewById(R.id.layout_titlebar_rootlayout);
//设置背景颜色
layout_titlebar_rootlayout.setBackgroundColor(mColor);
//设置标题文字颜色
tv_titlebar_title.setTextColor(mTextColor);
setTitle(titleName);
public void setTitle(String titlename)
if (!TextUtils.isEmpty(titlename))
tv_titlebar_title.setText(titlename);
public void setLeftListener(OnClickListener onClickListener)
iv_titlebar_left.setOnClickListener(onClickListener);
public void setRightListener(OnClickListener onClickListener)
getIv_titlebar_right.setOnClickListener(onClickListener);
//iv_titlebar_right.setOnClickListener(onClickListener);
以上是关于Android自定义View之自定义组合控件的主要内容,如果未能解决你的问题,请参考以下文章
Android自定义View实战之自定义评价打分控件RatingBar,可以自定义星星大小和间距
Android进阶之自定义View实战仿iOS UISwitch控件实现