Android custom layout and android Layout

Source: Internet
Author: User
Tags getcolor

Android custom layout and android Layout
1. Create an attribute File

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="topbar">        <attr name="title" format="string"/>        <attr name="titleTextSize" format="dimension" />        <attr name="titleTextColor" format="color" />                <attr name="leftText" format="string"/>        <attr name="leftTextBackgroud"  format="reference|color" />        <attr name="leftTextColor" format="color" />                 <attr name="rightText" format="string"/>        <attr name="rightTextBackgroud"  format="reference|color" />        <attr name="rightTextColor" format="color" />    </declare-styleable></resources>


2. Create a custom Layout class
Public class TopBar extends RelativeLayout {// custom control private Button leftButton, rightButton; private TextView tvTitle; // custom attribute private int leftTextColor; private Drawable leftBackGround; private String leftText; private int rightTextColor; private Drawable rightBackGround; private String rightText; private String titleText; private float titleTextSize; private int titleTextColor; private topBarListener tListener; public void listener (topBarListener tListener) {this. tListener = tListener;} public interface topBarListener {public void clickLeft (); public void clickRight ();} private LayoutParams leftParams, rightParams, titleParams; @ SuppressLint ("NewApi ") public TopBar (Context context, AttributeSet attrs) {super (context, attrs); // TODO Auto-generated constructor stub // get the attribute value TypedArray ta = context. obtainStyledAttributes (attrs, R. styleable. topbar); leftTextColor = ta. getColor (R. styleable. topbar_leftTextColor, 0); leftBackGround = ta. getDrawable (R. styleable. topbar_leftTextBackgroud); leftText = ta. getString (R. styleable. topbar_leftText); rightTextColor = ta. getColor (R. styleable. topbar_rightTextColor, 0); rightBackGround = ta. getDrawable (R. styleable. topbar_rightTextBackgroud); rightText = ta. getString (R. styleable. topbar_rightText); titleText = ta. getString (R. styleable. topbar_title); titleTextSize = ta. getDimension (R. styleable. topbar_titleTextSize, 0); titleTextColor = ta. getColor (R. styleable. topbar_titleTextColor, 0); ta. recycle (); // initialization control leftButton = new Button (context); rightButton = new Button (context); tvTitle = new TextView (context); leftButton. setText (leftText); leftButton. setBackground (leftBackGround); leftButton. setTextColor (leftTextColor); rightButton. setText (rightText); rightButton. setTextColor (rightTextColor); rightButton. setBackground (rightBackGround); tvTitle. setText (titleText); tvTitle. setTextSize (titleTextSize); tvTitle. setTextColor (titleTextColor); tvTitle. setGravity (Gravity. CENTER); setBackgroundColor (0xfff5346); leftParams = new LayoutParams (ViewGroup. layoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); leftParams. addRule (RelativeLayout. ALIGN_PARENT_LEFT, TRUE); addView (leftButton, leftParams); rightParams = new LayoutParams (ViewGroup. layoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT); rightParams. addRule (RelativeLayout. ALIGN_PARENT_RIGHT, TRUE); addView (rightButton, rightParams); titleParams = new LayoutParams (ViewGroup. layoutParams. WRAP_CONTENT, LayoutParams. MATCH_PARENT); titleParams. addRule (RelativeLayout. CENTER_IN_PARENT, TRUE); addView (tvTitle, titleParams); leftButton. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubtListener. clickLeft () ;}}); rightButton. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View arg0) {// TODO Auto-generated method stubtListener. clickRight ();}});}}


3. Create a listener event and use
public class MainActivity extends Activity {TopBar top  ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);top = (TopBar)findViewById(R.id.myTop);top.settListener(new topBarListener() {@Overridepublic void clickRight() {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "clicked the right Button!", 1000).show();}@Overridepublic void clickLeft() {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), "clicked the left Button!", 1000).show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"       xmlns:custom="http://schemas.android.com/apk/res/com.example.topbardemo"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    >    <com.example.topbardemo.TopBar         android:id="@+id/myTop"    android:layout_width="match_parent"    android:layout_height="40dp"    custom:leftText="left"    custom:leftTextColor="#003300"    custom:leftTextBackgroud="#00BFFF"    custom:title="title"    custom:titleTextColor="#000000"    custom:titleTextSize="14sp"    custom:rightText="right"    custom:rightTextColor="#000000"     custom:rightTextBackgroud="#00BFFF">    </com.example.topbardemo.TopBar></RelativeLayout>



Several points of attention: 1, in use need of custom reference value, xmlns: custom = "http://schemas.android.com/apk/res/xxx" xxx is the project package name 2, the use of TypedArray needs to release resources ta. recycle ();
:

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.