1、闲聊
上一篇博客我们已经聊了聊组合控件了,这里我就不瞎BB了,没有看过的,可以去看一下Android 组合控件之删除文本框
http://blog.youkuaiyun.com/ty_phone8/article/details/77916268
2、实例
说不BB就不BB,来我们会看另一种组合控件的实现代码:
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="wrap_content"
android:background="#dcdcdc"
android:gravity="center"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/customcontrol_title_bar_left_rl"
android:layout_width="44dp"
android:layout_height="44dp"
android:gravity="center">
<ImageView
android:id="@+id/customcontrol_title_bar_left_iv"
android:layout_width="22dp"
android:layout_height="22dp"
android:src="@drawable/custom_control_back" />
</RelativeLayout>
<TextView
android:id="@+id/customcontrol_title_bar_center_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="This is a title."
android:textColor="#6976bb"
android:textSize="18sp" />
<RelativeLayout
android:id="@+id/customcontrol_title_bar_right_rl"
android:layout_width="44dp"
android:layout_height="44dp"
android:gravity="center">
<ImageView
android:id="@+id/customcontrol_title_bar_right_iv"
android:layout_width="22dp"
android:layout_height="22dp"
android:src="@drawable/custom_control_plus" />
</RelativeLayout>
</LinearLayout>
自定义:
public class TitleBar extends RelativeLayout {
private View titleBar;
private RelativeLayout leftGroup;
private ImageView leftImg;
private TextView centerTxt;
private RelativeLayout rightGroup;
private ImageView rightImg;
public TitleBar(Context context) {
super(context);
init(context);
}
public TitleBar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public TitleBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
private <T extends View> T findTitleBarById(int resId) {
return (T) titleBar.findViewById(resId);
}
private void init(Context context) {
titleBar = LayoutInflater.from(context).inflate(R.layout.customcontrol_titlebar, null);
leftGroup = findTitleBarById(R.id.customcontrol_title_bar_left_rl);
leftImg = findTitleBarById(R.id.customcontrol_title_bar_left_iv);
centerTxt = findTitleBarById(R.id.customcontrol_title_bar_center_tv);
rightGroup = findTitleBarById(R.id.customcontrol_title_bar_right_rl);
rightImg = findTitleBarById(R.id.customcontrol_title_bar_right_iv);
leftGroup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (null != onClickTitleBarListener) {
onClickTitleBarListener.onClickLeft(v);
}
}
});
rightGroup.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (null != onClickTitleBarListener) {
onClickTitleBarListener.onClickRight(v);
}
}
});
centerTxt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (null != onClickTitleBarListener) {
onClickTitleBarListener.onClickTitle(v);
}
}
});
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
titleBar.setLayoutParams(lp);
addView(titleBar);
}
public void setTitleBarLeftImage(Object obj) throws RuntimeException {
if (null != leftImg) {
if (obj instanceof Integer) {
leftImg.setImageResource((Integer) obj);
} else if (obj instanceof Bitmap) {
leftImg.setImageBitmap((Bitmap) obj);
} else if (obj instanceof Drawable) {
leftImg.setImageDrawable((Drawable) obj);
} else if (obj instanceof Uri) {
leftImg.setImageURI((Uri) obj);
} else {
throw new RuntimeException("Please pass the parameters correctly.");
}
} else {
throw new RuntimeException("The control was not found.");
}
}
public void setTitleBarRightImage(Object obj) throws RuntimeException {
if (null != rightImg) {
if (obj instanceof Integer) {
rightImg.setImageResource((Integer) obj);
} else if (obj instanceof Bitmap) {
rightImg.setImageBitmap((Bitmap) obj);
} else if (obj instanceof Drawable) {
rightImg.setImageDrawable((Drawable) obj);
} else if (obj instanceof Uri) {
rightImg.setImageURI((Uri) obj);
} else {
throw new RuntimeException("Please pass the parameters correctly.");
}
} else {
throw new RuntimeException("The control was not found.");
}
}
public void setTitleBarCenterText(String text) throws RuntimeException {
if (null != centerTxt) {
centerTxt.setText(text);
} else {
throw new RuntimeException("The control was not found.");
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void setTitleBarBackground(Object obj) throws RuntimeException {
if (null != titleBar) {
if (obj instanceof Integer) {
titleBar.setBackgroundResource((Integer) obj);
} else if (obj instanceof Drawable) {
titleBar.setBackground((Drawable) obj);
} else {
throw new RuntimeException("Please pass the parameters correctly.");
}
} else {
throw new RuntimeException("The control was not found.");
}
}
public ImageView getLeftImg() {
return leftImg;
}
public TextView getCenterTxt() {
return centerTxt;
}
public ImageView getRightImg() {
return rightImg;
}
private onClickTitleBarListener onClickTitleBarListener;
public void setOnClickTitleBarListener(TitleBar.onClickTitleBarListener onClickTitleBarListener) {
this.onClickTitleBarListener = onClickTitleBarListener;
}
public interface onClickTitleBarListener {
void onClickLeft(View v);
void onClickRight(View v);
void onClickTitle(View v);
}
}
demo调用:
TitleBar titleBar = (TitleBar) findViewById(R.id.titleBar);
titleBar.setTitleBarCenterText("标题");
titleBar.setTitleBarLeftImage(R.drawable.custom_control_plus);
titleBar.setTitleBarRightImage(R.drawable.custom_control_back);
titleBar.setTitleBarBackground(android.R.color.holo_blue_light);
titleBar.setOnClickTitleBarListener(new TitleBar.onClickTitleBarListener() {
@Override
public void onClickLeft(View v) {
toask_make("This is the picture on the left.");
}
@Override
public void onClickRight(View v) {
toask_make("Here is the picture on the right.");
}
@Override
public void onClickTitle(View v) {
toask_make("This is the title section.");
}
});
3、demo资源
开源地址:
组合控件我们就聊到这里,两种方法都介绍了一下,删除文本框我们使用的是纯代码,这个组合控件我们使用的是xml + java实现
欢迎大家指正、分享