Android Custom Controls

Source: Internet
Author: User

Create a new layout file for the custom control in layout (typically the LinearLayout layout) code as follows: <?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "50DP"
android:background= "@mipmap/icon_title" >

<button
Android:id= "@+id/btnuser"
Android:layout_width= "50DP"
android:layout_height= "50DP"
android:background= "@mipmap/icon_body"/>

<textview
Android:id= "@+id/tvtitle"
Android:layout_width= "0DP"
android:layout_height= "Match_parent"
android:layout_weight= "1"
android:gravity= "Center"
android:text= "Home"
Android:textsize= "25SP"/>

<button
Android:id= "@+id/btnconfig"
Android:layout_width= "50DP"
android:layout_height= "50DP"
android:background= "@mipmap/icon_left"/>

</LinearLayout> then create a new control class that inherits from the LinearLayout code as follows: Package com.example.flypie.notesbook.Layout;

Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.LinearLayout;
Import Android.widget.TextView;
Import Android.widget.Toast;

Import COM.EXAMPLE.FLYPIE.NOTESBOOK.R;

/**
* Created by Flypie on 2015/12/9.
*/
public class Titlelayout extends LinearLayout implements view.onclicklistener{

Button Btnuser;
Button Btnconfig;
TextView Tvtitle;

Public Titlelayout (context context, AttributeSet Attrs) {
Super (context, attrs);
Layoutinflater.from (context). Inflate (R.layout.menu_title, this);
Initview ();
Setlistener ();
}


private void Setlistener () {
Btnuser.setonclicklistener (this);
Btnconfig.setonclicklistener (this);
}

private void Initview () {
Btnuser= (Button) Findviewbyid (R.id.btnuser);
Btnconfig= (Button) Findviewbyid (r.id.btnconfig);
}

@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.btnuser:
Toast.maketext (GetContext (), "Btnuser", Toast.length_short). Show ();
Break
Case R.id.btnconfig:
Toast.maketext (GetContext (), "Btnconfig", Toast.length_short). Show ();
Break
Default
Break
}
}
Finally, just introduce the control where you need it: <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= "Match_parent"
Tools:context= ". Mainactivity ">

<com.example.flypie.notesbook.layout.titlelayout
Android:id= "@+id/zdytitle"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >

</com.example.flypie.notesbook.Layout.TitleLayout>

<listview
Android:id= "@+id/lvnotes"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:layout_below= "@+id/zdytitle" >

</ListView>
</RelativeLayout>

Where the com.example.flypie.notesbook.Layout.TitleLayout is a custom control

Android Custom Controls

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.