Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Custom Android UI template Comments     - JSON data normalization (normalize) (Programming)

- 2 minutes to read large data framework Hadoop and Spark similarities and differences (Server)

- Java Generics Introduction --HashMap summary (Programming)

- MD5 and simple to use (Linux)

- Nagios (centreon) monitoring Linux Log (Server)

- CentOS7 install MySQL 5.5 (Database)

- Oracle table space is too large processing time (Database)

- Linux foundation tutorial: how to modify the host name on CentOS or RHEL 7 (Linux)

- Linux operating system security settings initial understanding (Linux)

- Linux screen commonly commands (Linux)

- About DataGuard three protected mode test (Database)

- Use preseed script to install Ubuntu 14.04 (Linux)

- Ubuntu rights management common commands (Linux)

- Linux - Common process the command (Linux)

- How to install and configure in Ubuntu 14.10 'Weather Information Indicator' (Linux)

- Linux Shell Scripting multithreading (Programming)

- Java garbage collection (Programming)

- Linux Mint 17.2 64 bit installation Docker and management software seagull (Linux)

- CentOS 6.5 three ways to configure the IP address (Linux)

- phpinfo (): like phpinfo as a Python script (Programming)

  Custom Android UI template Comments
  Add Date : 2017-04-13      
  I do not know if you have had a custom UI templates in the actual development? Today, we take the time to study a little custom Android UI template to share with you.
Each well-designed App is custom title bar, the title bar of the custom during the majority of people may have been a title custom xml file, and then where necessary by direct reference to include, in this than each layout file write the title bar has evolved a lot, but still not the most simple and effective way, why can not we customize a header control it? Today will take you own a title bar control.

Start it:

The first step: custom xml attributes

Create a new android project, create a new file in the values atts.xml folder, declare a property we will need to specify when using the custom control in the xml file.

< ? Xml version = "1.0" encoding = "utf-8"?>
< Resources>

    < Declare-styleable name = "ToolBar">
        < Attr name = "title" format = "string" />
        < Attr name = "titleTextSize" format = "dimension" />
        < Attr name = "titleTextColor" format = "color" />
        < Attr name = "leftBackground" format = "reference | color" />
        < Attr name = "leftText" format = "string" />
        < Attr name = "leftTextColor" format = "reference | color" />
        < Attr name = "rightBackground" format = "reference | color" />
        < Attr name = "rightText" format = "string" />
        < Attr name = "rightTextColor" format = "reference | color" />
    < / Declare-styleable>

< / Resources>
In front of the name attribute is the name we want to use, the back of the format shows the format of the value of the property received, string represents the value of the property is a string, color represents the value of the property is a color value, dimension represents the property value is a size, reference represents the value of the property may be a reference to a resource id, as well as other common format value: boolean (Boolean value), float (floating point value), integer (integer), fraction (percentage) , enum (enumerated value), flag (or bit operation).

Step Two: Customize the class title
Customizing the Java file a class inherits RelativeLayout, and to achieve its constructor, our title bar consists of three parts, on each side is a Button, the middle is a TextView, so we need to do things in this layout file that these three controls for processing.

To declare the title bar and the three space-related parameters that are set in accordance with atts.xml, because we are quoted from the xml when referenced custom control, set the properties are in the xml file, we get the value of the property from the xml file and then set up the assignment of control.

/ **
* Three title bar controls
* /
private Button leftBtn, rightBtn;
private TextView title;

/ **
* Left button of the property
* /
private int leftTextColor;
private Drawable leftBackground;
private String leftText;

/ **
* Button to the right of property
* /
private int rightTextColor;
private Drawable rightBackground;
private String rightText;

/ **
* Middle TextView properties
* /
private int titleTextColor;
private String titleText;
private float titleTextSize;

/ **
* Layout parameters of three controls
* /
private LayoutParams leftParams, rightParams, titleParams;
Here is the constructor, the constructor passes in two parameters, a context parameter, and the other is AttributeSet, AttributeSet is a collection of attributes, it can deal with a set of xml tag collection. Use ta.getXXX method, we can start with the xml file for the value of the property, and then set these values to the control. Finally, set the controls by LayoutParams width and height, set the width and height after calling addView method, add controls.

public MyToolBar (Context context, AttributeSet attrs) {
super (context, attrs);
TypedArray ta = context.obtainStyledAttributes (attrs,

leftTextColor = ta.getColor (R.styleable.ToolBar_leftTextColor, 0);
leftBackground = ta.getDrawable (R.styleable.ToolBar_leftBackground);
leftText = ta.getString (R.styleable.ToolBar_leftText);

rightTextColor = ta.getColor (R.styleable.ToolBar_rightTextColor, 0);
rightBackground = ta.getDrawable (R.styleable.ToolBar_rightBackground);
rightText = ta.getString (R.styleable.ToolBar_rightText);

titleText = ta.getString (R.styleable.ToolBar_title);
titleTextColor = ta.getColor (R.styleable.ToolBar_titleTextColor, 0);
titleTextSize = ta.getDimension (R.styleable.ToolBar_titleTextSize, 0);

// Ta for recycling
ta.recycle ();

leftBtn = new Button (context);
rightBtn = new Button (context);
title = new TextView (context);

/ **
* Set properties
* /
leftBtn.setText (leftText);
leftBtn.setTextColor (leftTextColor);
leftBtn.setBackground (leftBackground);

rightBtn.setText (rightText);
rightBtn.setTextColor (rightTextColor);
rightBtn.setBackground (rightBackground);

title.setText (titleText);
title.setTextColor (titleTextColor);
title.setTextSize (titleTextSize);
title.setGravity (Gravity.CENTER);

// Set the background color of the whole
setBackgroundColor (0x7CFC0055);

leftParams = new LayoutParams (
leftParams.addRule (RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
// Add controls
addView (leftBtn, leftParams);

rightParams = new LayoutParams (
rightParams.addRule (RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
addView (rightBtn, rightParams);

titleParams = new LayoutParams (
titleParams.addRule (RelativeLayout.CENTER_IN_PARENT, TRUE);
addView (title, titleParams);

The third step: We define the reference control
Since the definition of good controls, we can use custom controls, and cite our custom controls in the main xml file layout. The first three are based on custom android: beginning, which indicates that these attributes are system attributes back to the beginning custombar showing these properties are our custom, in order to be able to use a custom custombar, we We need to add one in the RelativeLayout:

xmlns: custombar = "http://schemas.android.com/apk/res/com.example.mytoolbar"
Note com.example.mytoolbar is behind the name of your application package. If not, but you are using eclipse android studio, then this line not too much trouble, simply write on:

xmlns: custombar = "http://schemas.android.com/apk/res-auto"
We are our atts.xml declared the property to set custom attributes.

< RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
    xmlns: custombar = "http://schemas.android.com/apk/res/com.example.mytoolbar"
    xmlns: tools = "http://schemas.android.com/tools"
    android: layout_width = "match_parent"
    android: layout_height = "match_parent">

    < Com.example.mytoolbar.MyToolBar
        android: id = "@ + id / mytoolbar"
        android: layout_width = "match_parent"
        android: layout_height = "48dp"
        custombar: leftBackground = "@ android: color / holo_blue_light"
        custombar: leftText = "return"
        custombar: leftTextColor = "@ android: color / black"
        custombar: rightBackground = "@ android: color / holo_blue_light"
        custombar: rightText = "More"
        custombar: rightTextColor = "@ android: color / black"
        custombar: title = "Title Bar"
        custombar: titleTextColor = "@ android: color / black"
        custombar: titleTextSize = "18sp">
    < /com.example.mytoolbar.MyToolBar>

< / RelativeLayout>
After completing such work, run your project, we can see that the picture at the beginning of the article gives.

Step Four: To add custom event

It seems to have something less, yes, we have not control click event. Event set to give a click event, you need to declare an event interface in a custom control and declare an instance of an interface:

private OnToolBarClickListener listener;
public interface OnToolBarClickListener {
/ **
* Left button click event
* /
public void leftClick ();

/ **
* Click the button to the right of the event
* /
public void rightClick ();
Then exposed to other classes call a method, this method is that the parameters of this interface:

public void setOnToolBarClickListener (OnToolBarClickListener listener) {
this.listener = listener;
Interface method calls last about two-button click event to smart mode Tell me Guess what it is?

leftBtn.setOnClickListener (new OnClickListener () {

public void onClick (View v) {
listener.leftClick ();
rightBtn.setOnClickListener (new OnClickListener () {

public void onClick (View v) {
listener.rightClick ();
Methods written, we look at MainActivity call in:

public class MainActivity extends Activity {

private MyToolBar toolBar;
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        . GetActionBar () hide ();
        this.toolBar = (MyToolBar) this.findViewById (R.id.mytoolbar);
        toolBar.setOnToolBarClickListener (new OnToolBarClickListener () {

public void rightClick () {
Toast.makeText (MainActivity.this, "to the right click", Toast.LENGTH_LONG) .show ();

public void leftClick () {
Toast.makeText (MainActivity.this, "left click", Toast.LENGTH_LONG) .show ();
- Build a super simple "hardware" firewall router (Linux)
- RedHat command line and graphical interface switching (Linux)
- Spring WebSocket Comments (Programming)
- How VirtualBox and VMware virtual machine conversion (Linux)
- What is a logical partition management LVM, how to use in Ubuntu (Linux)
- Oracle 11g upgrade PSU detailed steps (Database)
- Linux environment to configure Apache + Django + wsgi (Server)
- how to quickly find a file on Linux system (Linux)
- Mount and unloading disks under Linux (Linux)
- Linux into single user mode to modify the administrator password (Linux)
- Convert MySQL date string to a NULL value exception handling (Database)
- Java to create a table in the database SYBase (Database)
- VMware Workstation + Ubuntu 10.04 Download the Android 2.2 source code (Linux)
- Oracle database NUMBER (x, y) data types (Database)
- init level settings for Linux / etc / inittab file into six (restart) or does not support the level of solution (Linux)
- Upgrading to MySQL 5.7 partitioning problem solving (Database)
- To install the latest version of the EPEL on CentOS 5.x or 6.x (Linux)
- C ++ virtual functions Classic Insights (Programming)
- Git delete files (Linux)
- Android Launcher3 Application List Modify a transparent background (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.