Android Composite Control Custom Title Bar
- 2021-10-24 23:53:56
- OfStack
In this article, we share the simple custom title bar of Android for your reference. The specific contents are as follows
android custom controls have always been the biggest headache for developers, but we should have the spirit of facing difficulties.
MainActivity
package com.example.customview;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
/*
android Custom title combination control
Steps:
1. First, write the layout of the required functions xml Who is the parent control of the parsing layout?
Such as horizontal layout The parent control should be linearlayout More appropriate
2. Create a custom control class and inherit xml Parent control
3. Use in the constructor layoutInflat Dynamic loading layout
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Remove the self-contained title bar
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
}
TitleLayout.class
package com.example.customview.custom;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.customview.R;
/**
* Custom title bar And assign a click event
*/
public class TitleLayout extends LinearLayout implements View.OnClickListener {
private Button btback, btopen;
private TextView tvtitle;
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// Dynamic loading of title bar layout
LayoutInflater.from(context).inflate(R.layout.custom_layout, this);
initView();
}
private void initView() {// Initialize control
btback = (Button) findViewById(R.id.btback);
btback.setOnClickListener(this);
btopen = (Button) findViewById(R.id.btopen);
btopen.setOnClickListener(this);
tvtitle = (TextView) findViewById(R.id.tvtitle);
tvtitle.setOnClickListener(this);
}
@Override
public void onClick(View view) {// Listen for click events
switch (view.getId()) {
case R.id.btback:
((Activity) getContext()).finish();
Toast.makeText(getContext(), " Destroy the current Activity", Toast.LENGTH_SHORT).show();
break;
case R.id.btopen:
Toast.makeText(getContext(), " Unfold ", Toast.LENGTH_SHORT).show();
break;
case R.id.tvtitle:
Toast.makeText(getContext(), " Title ", Toast.LENGTH_SHORT).show();
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.customview.MainActivity">
<include layout="@layout/custom_layout" />
<com.example.customview.custom.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
custom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.customview.MainActivity">
<include layout="@layout/custom_layout" />
<com.example.customview.custom.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Paste the above code and you can run it.