TitleBar of android-oldman, android-oldman

Source: Internet
Author: User

TitleBar of android-oldman, android-oldman

With the increasing taste of the masses, the demand for app development is also constantly increasing, and there are more and more developers who want to show on the app, some early android controls gradually failed to meet development requirements. For example, TitleBar applications were not as many as they were originally. Later, Google launched ActionBar, later, ToolBar was introduced. to meet the increasing development needs.

This article aims to review some of the original technologies. As for the new technologies, the truth is actually the same. It is easy to understand the key points.

Next, let's talk about how to customize a TitleBar to implement some functions: 1. You can set different titles for TitleBar in any Activity that uses this uniform style. 2. In different titles, the back button is displayed as needed. 3. In different activities, set the titleBar to display pictures and text on the left, and click to execute the action.

Sample to be implemented:

Feature: Place TitleBarActiviti. class in an util folder. Where to use it, it is directly referenced in an xml layout file like this class (just like using TextView .). You can set the left-side backend to not display, and the right-side refresh to not display. You can set the titleBar style by yourself.

1 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "48dp" 5 android: gravity = "center_horizontal" 6 android: background = "# ffffff"> 7 8 <View 9 android: id = "@ + id/view_line" 10 android: layout_width = "match_parent" 11 android: layout_height = "1dp" 12 android: layout_alignParentBottom = "true" 13 android: background = "# 0000ff"/> 14 15 <RelativeLayout16 android: id = "@ + id/rel_titlebar" 17 android: layout_width = "match_parent" 18 android: layout_height = "match_parent"> 19 20 <TextView21 android: id = "@ + id/TV _back" 22 android: layout_width = "45dp" 23 android: layout_height = "match_parent" 24 android: layout_alignParentLeft = "true" 25 android: background = "#66ff0000" 26 android: gravity = "center" 27 android: focusable = "true" 28 android: clickable = "true" 29 android: text = "" 30 android: visibility = "invisible"/> 31 32 <View33 android: id = "@ + id/view_left" 34 android: layout_width = "1dp" 35 android: layout_height = "match_parent" 36 android: layout_toRightOf = "@ id/TV _back" 37 android: background = "#6600ff00" 38 android: visibility = "invisible"/> 39 40 <TextView41 android: id = "@ + id/TV _other" 42 android: layout_width = "45dp" 43 android: layout_height = "match_parent" 44 android: layout_alignParentRight = "true" 45 android: background = "#66ff0000" 46 android: gravity = "center" 47 android: text = "refresh" 48 android: focusable = "true" 49 android: clickable = "true" 50 android: visibility = "invisible"/> 51 52 <View53 android: id = "@ + id/view_right" 54 android: layout_width = "1dp" 55 android: layout_height = "match_parent" 56 android: layout_toLeftOf = "@ id/TV _other" 57 android: background = "#6600ff00" 58 android: visibility = "invisible"/> 59 60 <TextView61 android: id = "@ + id/TV _title_name" 62 android: layout_width = "45dp" 63 android: layout_height = "match_parent" 64 android: layout_toLeftOf = "@ id/view_right" 65 android: layout_toRightOf = "@ id/view_left" 66 android: background = "#6600ff00" 67 android: gravity = "center" 68 android: text = "Title_Name"/> 69 </RelativeLayout> 70 71 </RelativeLayout>

1 package com. example. ttilebardemo; 2 3 import android. app. activity; 4 import android. content. context; 5 import android. graphics. color; 6 import android. util. attributeSet; 7 import android. view. layoutInflater; 8 import android. view. view; 9 import android. view. view. onClickListener; 10 import android. widget. relativeLayout; 11 import android. widget. textView; 12 import android. widget. toast; 13 14 public cl Ass TitleBarActivity extends RelativeLayout implements OnClickListener {15 16 private Context mContext; 17 private View left, bottom, right; 18 private TextView backTextView, titleName, refersh; 19 private OnClickListener onClickListener; 20 private boolean canBack = false; 21 22 public TitleBarActivity (Context context) {23 this (context, null); 24 25} 26 27 public TitleBarActivity (Context context Context, Attri ButeSet attr) {28 super (context, attr); 29 mContext = context; 30 init (); 31} 32 33 private void init () {34 LayoutInflater. from (mContext ). inflate (R. layout. activity_titlebar, this, true); 35 // initialize view36 left = findViewById (R. id. view_left); 37 bottom = findViewById (R. id. view_line); 38 right = findViewById (R. id. view_right); 39 // initialize textview40 backTextView = (TextView) findViewById (R. id. TV _back); 41 backTex TView. setOnClickListener (this); 42 titleName = (TextView) findViewById (R. id. TV _title_name); 43 refersh = (TextView) findViewById (R. id. TV _other); 44 45} 46 47/** 48 * Set titleBar name 49 */50 public void setTitleName (String name) {51 titleName. setText (name); 52} 53 54/** 55 * set the visibility of the left side of titleBar 56 */57 public void setBackVisible () {58 backTextView. setVisibility (View. VISIBLE); 59 left. setVisibility (View. VISIBLE); 60 61} 62 63 64/** 65 * set the right font to be visible 66 */67 public void setRefeshVisible () {68 refersh. setVisibility (View. VISIBLE); 69 right. setVisibility (View. VISIBLE); 70} 71/** 72 * set the listener to be refreshed on the left. 73 */74 public void setRefreshListener (OnClickListener onRefresh, boolean canBack) {75 refersh. setOnClickListener (onRefresh); 76 this. canBack = canBack; 77} 78 79 80/** 81 * set the button click event on the left. 82 */83 84 @ Override85 public void onClick (View v) {86 if (v. getId () = R. id. TV _back & backTextView. isShown () & canBack) {87 backTextView. setBackgroundColor (Color. GREEN); 88} 89 90} 91 92}


Now, titleBar has been defined and will be used later.

3. Reference Titlebar in the xml layout file of the class

Activity_main.xml

1 <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" 2 xmlns: tools = "http://schemas.android.com/tools" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: gravity = "center_horizontal" 6 android: background = "# e7e7e7"> 7 8 <com. example. ttilebardemo. titleBarActivity 9 android: id = "@ + id/title_bar" 10 android: layout_width = "match_parent" 11 android: layout_height = "wrap_content" 12> 13 </com. example. ttilebardemo. titleBarActivity> 14 15 <TextView16 android: id = "@ + id/TV _haha" 17 android: layout_width = "match_parent" 18 android: layout_height = "wrap_content" 19 android: layout_marginTop = "50dp" 20 android: gravity = "center" 21 android: layout_below = "@ id/title_bar" 22 android: text = "hello, titleBar "/> 23 24 </RelativeLayout>

4. Write Titlebar in the Code as required.

1 package com. example. ttilebardemo; 2 3 import android. annotation. suppressLint; 4 import android. app. activity; 5 import android. OS. bundle; 6 import android. util. log; 7 import android. view. menu; 8 import android. view. menuItem; 9 import android. view. view; 10 import android. view. view. onClickListener; 11 import android. widget. textView; 12 import android. widget. toast; 13 14 public class MainActivity extends Activity {15 private TextView tvHa; 16 @ Override17 protected void onCreate (Bundle savedInstanceState) {18 super. onCreate (savedInstanceState); 19 setContentView (R. layout. activity_main); 20 initView (); 21} 22 23 private void initView () {24 tvHa = (TextView) findViewById (R. id. TV _haha); 25 TitleBarActivity titleBar = (TitleBarActivity) findViewById (R. id. title_bar); 26 titleBar. setBackVisible (); 27 titleBar. setTitleName ("set in window"); 28 titleBar. setRefeshVisible (); 29 30 titleBar. setRefreshListener (new OnClickListener () {31 32 public void onClick (View v) {33 Log. d ("reference page:", "kwg ........ "); 34} 35}, true); 36} 37}

Okay, that's it.

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.