Custom title with back button

1. Basically, every page in the project needs to use the return button and title. It becomes very troublesome and unnecessary to write every time it is used, so today I need to customize a TitleView for easy use.

First, write a layout file: put a back button and a title in it

<?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">
    <RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
        <ImageView
android:id="@+id/back_img"
android:layout_width="40dp"
android        
                
                                    :layout_height="40dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@mipmap/back" />
        <TextView
android:id="@+id/title_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="标题"
android:textSize="20sp" />
    </RelativeLayout>
</LinearLayout                                    
                                                                        >

Then customize a TitleView class, which contains the callback of the return button, and the setting of the title

public class TitleView extends FrameLayout {
private TextView titleText;
private ImageView back;
private setBackClickListener setBackClickListener;
public TitleView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title_layout, this);
            
                            titleText = findViewById(R.id.title_name);
back = findViewById(R.id.back_img);
back.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setBackClickListener.backClick();
            }
        });
    }
/**
     * 设置标题
     *
     * @param text
*/
                                                        
             public void setTitleText(String text) {
titleText.setText(text);
    }
/**
     * 隐藏返回按钮
     */
public void hideBackImage() {
back.setVisibility(View.GONE);
    }
public void setBackImageListener(setBackClickListener setBackClickListener) {
this.setBackClickListener = setBackClickListener;
    }
        
                
            
    public interface setBackClickListener {
        void backClick();
    }

}

最后是使用:在布局文件;

<com.newdicooker.tempetek.toastdemo.TitleView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="50dp" />

在代码中设置标题内容以及返回监听

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.title)
    TitleView title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setListener();

    }

    private void setListener() {
title.setTitleText("你好啊");
title.setBackImageListener(new TitleView.setBackClickListener() {
@Override
public void backClick() {
finish();
            }
        });
    }
                                                        
Create your own custom layout

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324853011&siteId=291194637