开发的项目中用到一个带标题的返回按钮,其实不难实现,一个ImageView和一个TextView足矣搞定,但是我们每个xml文件都这么写,就会有很多重复的代码,所以我们为了减少代码冗余,就自己自定义一个小控件。
思路:
新建一个类继承RelativeLayout,然后引入图片和文字的布局,在构造两个方法,设置图片的值和文字的值,以及点击事件即可。下面我们就看代码吧。
xml布局文件
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:background="@null">
<RelativeLayout
android:id="@+id/rl_backview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
<!--图片-->
<ImageView
android:id="@+id/iv_arrows"
android:layout_width="65dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
/>
<!-- 文字-->
<TextView
android:id="@+id/tv_column"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/iv_arrows"
android:textColor="@color/white"
android:textSize="18sp"
android:gravity="center_vertical" />
<!--android:layout_marginLeft="@dimen/news_lefttext_mleft"-->
</RelativeLayout>
</RelativeLayout>
新建的BackView的类:
package com.enlink.newsreader.view;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.enlink.newsreader.R;
/**
* 回退按钮
* 相当于按回退键
*/
public class BackView extends RelativeLayout {
private Context mContext;
private RelativeLayout rl_backview;
private TextView tv_column;
private ImageView iv_arrows;
public BackView(Context context) {
super(context);
if (!isInEditMode()) {
initView(context);
}
}
public BackView(Context context, AttributeSet attrs) {
super(context, attrs);
if (!isInEditMode()) {
initView(context);
}
}
public BackView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
if (!isInEditMode()) {
initView(context);
}
}
private void initView(Context context) {
mContext = context;
View backView = View.inflate(mContext, R.layout.backview, null);
rl_backview = (RelativeLayout) backView.findViewById(R.id.rl_backview);
iv_arrows = (ImageView) backView.findViewById(R.id.iv_arrows);
tv_column = (TextView) backView.findViewById(R.id.tv_column);
this.addView(backView);
if (mContext instanceof Activity) {
TitleOnClick titleOnClick = new TitleOnClick();
titleOnClick.setActivity((Activity) mContext);
rl_backview.setOnClickListener(titleOnClick);
}
}
public void setTextValue(String articleColumn) {
tv_column.setText(articleColumn);
}
public void setImageSource(int id) {
iv_arrows.setImageResource(id);
}
private class TitleOnClick implements OnClickListener {
private Activity activity;
private void setActivity(Activity activity) {
this.activity = activity;
}
@Override
public void onClick(View view) {
this.activity.finish();
}
}
}
最后是使用:
只要我们在我们想用到这个布局的地方用这个类即可,如下
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="@dimen/top_title_height"
android:background="@color/Azure">
<com.autochina.view.BackView
android:id="@+id/forget_pwd_backView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
</com.autochina.view.BackView>
</RelativeLayout>
然后在Activity中的onCreate方法中加上
BackView backView = (BackView) findViewById(R.id.backView);
backView.setImageSource(R.drawable.user_center_back);//set icon
backView.setTextValue("登入");//set title
他还可以变成别的功能的控件,只要把布局改一改,然后在BackView类中监听的方法改改,在构造一些新的功能的方法就可以了。
这种方法一定不是最简单的方法,所以大家不喜勿喷。