Android custom control-title bar reuse and Android control reuse

Source: Internet
Author: User

Android custom control-title bar reuse and Android control reuse

Consistent style, first look, and then implement


Compile the custom property file atts. xml. The attributes involved in the custom property include the background image of the buttons on both sides, the content of the middle title, font size, and font color.

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="TopBar">        <attr name="leftBackground" format="reference"/>        <attr name="rightBackground" format="reference"/>        <attr name="titleText" format="string"/>        <attr name="titleTextSize" format="dimension"/>        <attr name="titleTextColor" format="color|reference"/>    </declare-styleable></resources>

Write the layout file layout_topbar.xml, and use the relative layout. The left button is the same as the left alignment of the parent control and the rear margin is 5dp. the right button is the same, and the middle title is displayed in the center.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="wrap_content"    tools:context=".TopBar">    <Button        android:id="@+id/leftButton"        android:layout_width="30dp"        android:layout_height="30dp"        android:layout_alignParentLeft="true"        android:layout_centerVertical="true"        android:layout_marginLeft="5dp" />    <TextView        android:id="@+id/titleTextView"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_centerHorizontal="true"        android:gravity="center" />    <Button        android:id="@+id/rightButton"        android:layout_width="30dp"        android:layout_height="30dp"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:layout_marginRight="5dp" /></RelativeLayout>

Write custom controls, inherit RelativeLayout, get custom properties, and assign values to corresponding controls

Package cn.edu. zafu. view. topbar; import android. content. context; import android. content. res. typedArray; import android. util. attributeSet; import android. view. layoutInflater; import android. view. view; import android. widget. button; import android. widget. relativeLayout; import android. widget. textView;/*** Created by lizhangqu on 2015/1/18. */public class TopBar extends RelativeLayout {private Button leftBut Ton, rightButton; private TextView titleTextView; private OnLeftAndRightClickListener listener; // listens for click events // sets the listener public void setOnLeftAndRightClickListener (OnLeftAndRightClickListener listener) {this. listener = listener;} // set the visibility of the left button public void setLeftButtonVisibility (boolean flag) {if (flag) leftButton. setVisibility (VISIBLE); else leftButton. setVisibility (INVISIBLE);} // sets the visibility public void of the button on the right. SetRightButtonVisibility (boolean flag) {if (flag) rightButton. setVisibility (VISIBLE); else rightButton. setVisibility (INVISIBLE);} // click the interface public interface OnLeftAndRightClickListener {public void onLeftButtonClick (); public void onRightButtonClick ();} public TopBar (Context context, AttributeSet attrs) {super (context, attrs); LayoutInflater. from (context ). inflate (R. layout. layout_topbar, this); l EftButton = (Button) findViewById (R. id. leftButton); rightButton = (Button) findViewById (R. id. rightButton); titleTextView = (TextView) findViewById (R. id. titleTextView); leftButton. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {if (listener! = Null) listener. onLeftButtonClick (); // click callback}); rightButton. setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {if (listener! = Null) listener. onRightButtonClick (); // click callback}); // obtain the custom attribute and assign the value TypedArray typedArray = context. obtainStyledAttributes (attrs, R. styleable. topBar); int leftBtnBackground = typedArray. getResourceId (R. styleable. topBar_leftBackground, 0); int rightBtnBackground = typedArray. getResourceId (R. styleable. topBar_rightBackground, 0); String titleText = typedArray. getString (R. styleable. topBar_titleText); float titleTextSize = typedArray. getDimension (R. styleable. topBar_titleTextSize, 0); int titleTextColor = typedArray. getColor (R. styleable. topBar_titleTextColor, 0x38ad5a); typedArray. recycle (); // release the resource leftButton. setBackgroundResource (leftBtnBackground); rightButton. setBackgroundResource (rightBtnBackground); titleTextView. setText (titleText); titleTextView. setTextSize (titleTextSize); titleTextView. setTextColor (titleTextColor );}}


Call Custom Controls

<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: M = "http://schemas.android.com/apk/res-auto" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" tools: context = ". mainActivity "> <cn.edu. zafu. view. topbar. topBar android: id = "@ + id/topbar" android: layout_width = "match_parent" android: layout_height = "50dp" android: background = "# 38ad5a" custom: leftBackground = "@ drawable/left_button_selector" custom: rightBackground = "@ drawable/right_button_selector" M: titleText = "title content" custom: titleTextColor = "#000" custom: titleTextSize = "6sp"/> </RelativeLayout>



package cn.edu.zafu.view.topbar;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.widget.Toast;public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        TopBar topBar= (TopBar) findViewById(R.id.topbar);        topBar.setOnLeftAndRightClickListener(new TopBar.OnLeftAndRightClickListener() {            @Override            public void onLeftButtonClick() {                Toast.makeText(getApplicationContext(),"left",Toast.LENGTH_SHORT).show();            }            @Override            public void onRightButtonClick() {                Toast.makeText(getApplicationContext(),"right",Toast.LENGTH_SHORT).show();            }        });        topBar.setLeftButtonVisibility(false);    }}


The two selectors used in the preceding layout are as follows:
Write left_button_selector.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/left_pressed" android:state_pressed="true"/>    <item android:drawable="@drawable/left_normal" /></selector>


Compile right_button_selector.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/right_pressed" android:state_pressed="true"/>    <item android:drawable="@drawable/right_normal" /></selector>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.