Use of Android custom controls and android Custom Controls
First, define a resource file attrs. xml and declare the custom attributes.
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="TopBar"> <attr name="titleText" format="string"></attr> <attr name="titleTextSize" format="dimension"></attr> <attr name="titleTextColor" format="reference|color"></attr> <attr name="leftButtonText" format="string"></attr> <attr name="leftButtonTextSize" format="dimension"></attr> <attr name="leftButtonBackground" format="reference|color"></attr> <attr name="rightButtonText" format="string"></attr> <attr name="rightButtonTextSize" format="dimension"></attr> <attr name="rightButtonBackground" format="reference|color"></attr> </declare-styleable></resources>
Custom class TopBar, inheriting RelativeLayout for simplicity
Package com. sphere. topbar; import android. annotation. suppressLint; import android. content. context; import android. content. res. typedArray; import android. graphics. drawable. drawable; import android. util. attributeSet; import android. view. view; import android. view. viewGroup; import android. widget. button; import android. widget. relativeLayout; import android. widget. textView; import android. widget. toast; public class TopBar extends RelativeLayout {private String titleText; private float titleTextSize; private int titleTextColor; private String leftButtonText; private float vertex; private Drawable vertex; private String rightButtonText; private float vertex; private Drawable Vertex; private TextView titleTextView; private Button leftButton; private Button RightButton; private LayoutParams listener; private TopBarOnClickListener listener; // defines the click response Event Callback interface TopBarOnClickListener {public abstract void onLeftButtonClicked (); public abstract void onRightButtonClicked () ;}@ SuppressLint ("NewApi") public TopBar (Context context, AttributeSet attrs) {supe R (context, attrs); // obtain the custom attribute set TypedArray ta = context in the xml file. obtainStyledAttributes (attrs, R. styleable. topBar); // obtain the title attributes titleText = ta. getString (R. styleable. topBar_titleText); titleTextSize = ta. getDimension (R. styleable. topBar_titleTextSize, 0); titleTextColor = ta. getColor (R. styleable. topBar_titleTextColor, 0); // obtain the left button attributes leftButtonText = ta. getString (R. styleable. topBar_leftButt OnText); leftButtonTextSize = ta. getDimension (R. styleable. topBar_leftButtonTextSize, 0); leftButtonBackground = ta. getDrawable (R. styleable. topBar_leftButtonBackground); // obtain the right button attributes rightButtonText = ta. getString (R. styleable. topBar_rightButtonText); rightButtonTextSize = ta. getDimension (R. styleable. topBar_rightButtonTextSize, 0); rightButtonBackground = ta. getDrawable (R. styleable. topBar _ RightButtonBackground); // initial inner basic component leftButton = new Button (context); rightButton = new Button (context); titleTextView = new TextView (context); // set the attribute titleTextView. setText (titleText); titleTextView. setTextSize (titleTextSize); titleTextView. setTextColor (titleTextColor); titleLayoutParams = new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); titleLayoutParam S. addRule (RelativeLayout. CENTER_IN_PARENT); titleTextView. setLayoutParams (titleLayoutParams); leftButton. setText (leftButtonText); leftButton. setTextSize (leftButtonTextSize); leftButton. setBackground (leftButtonBackground); leftBtnLayoutParams = new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); leftBtnLayoutParams. addRule (RelativeLayout. ALIGN_PARENT_LEFT, TRUE); leftButton. setLa YoutParams (leftBtnLayoutParams); rightButton. setText (rightButtonText); rightButton. setTextSize (rightButtonTextSize); rightButton. setBackground (rightButtonBackground); rightBtnLayoutParams = new LayoutParams (LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); rightBtnLayoutParams. addRule (RelativeLayout. ALIGN_PARENT_RIGHT, TRUE); rightButton. setLayoutParams (rightBtnLayoutParams); // Add to View addVi Ew (titleTextView); addView (leftButton); addView (rightButton); // addView (View child, ViewGroup. layoutParams params); ta. recycle (); leftButton. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {listener. onLeftButtonClicked () ;}}); rightButton. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {listener. onRightButtonClicked ();}});}/* ** Instance of the class calls this method * in the form of an anonymous internal class, the method in the real interface * @ param listener */public void setTopBarOnClickListener (TopBarOnClickListener listener) {this. listener = listener;}/*** set the navigation background color * @ param color */public void setTopBarBackgroundColor (int color) {this. setBackgroundColor (color);}/*** specifies whether the setting button is visible * @ param show */public void setLeftButtonVisible (boolean show) {leftButton. setVisibility (show? View. VISIBLE: View. GONE);} public void setRightButtonVisible (boolean show) {rightButton. setVisibility (show? View. VISIBLE: View. GONE );}}
Constructor with the Context context and AttributeSet attrs Parameters
Obtain the attributes from the xml file and set them to the corresponding control.
Don't forget to add the internal controls to the View by addView ().
Use custom controls in main. xml
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" xmlns: myTopBar = "http://schemas.android.com/apk/res-auto" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_vertical_margin" android: paddingBottom = "@ dimen/plugin" tools: context = ". mainActivity "> <com. sphere. topbar. topBar android: id = "@ + id/topbar" android: layout_width = "match_parent" android: layout_height = "48dp" myTopBar: titleText = "Custom title" myTopBar: titleTextColor = "#000" myTopBar: titleTextSize = "10sp" myTopBar: leftButtonText = "Back" myTopBar: Signature = "# ff00ff" myTopBar: Signature = "10sp" myTopBar: rightButtonText = "More" myTopBar: rightButtonBackground = "#00ff00" myTopBar: rightButtonTextSize = "10sp"/> </RelativeLayout>
Note to add a namespace, xmlns: myTopBar = "http://schemas.android.com/apk/res-auto"
For the ADT development environment, you need to write xmlns: myTopBar = "http://schemas.android.com/apk/res/com.sphere.topbar"
Note that although the name of myTopBar can be set as needed, android is not available. Otherwise, it will conflict with the system. During ADT citation, You need to append the full package name behind res.
Reference Custom Attributes starting with (myTopBar: property name.
MainActivity. java
package com.sphere.topbar;import android.graphics.Color;import android.support.v7.app.ActionBarActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.Toast;public class MainActivity extends ActionBarActivity { private TopBar mTopBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTopBar = (TopBar)findViewById(R.id.topbar); mTopBar.setBackgroundColor(Color.parseColor("#46B4CD")); mTopBar.setTopBarOnClickListener(new TopBar.TopBarOnClickListener() { @Override public void onLeftButtonClicked() { Toast.makeText(MainActivity.this,"Back",Toast.LENGTH_SHORT).show(); } @Override public void onRightButtonClicked() { Toast.makeText(MainActivity.this, "More", Toast.LENGTH_SHORT).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
Here we get the instance of the custom control and call the setTopBarOnClickListener (TopBarOnClickListener listener) method.
Add a click event listener for the button.
As follows: