"Android Custom Controls" linearlayout define Actionbar Styles

Source: Internet
Author: User

In fact, we see all actionbar plainly, is a custom linearlayout or relatedlayout; Practice Today LinearLayout

Custom.


There are several LinearLayout customization methods:

1. Customize the XML layout, then load the layout and customize a view inheritance LinearLayout

2. declare all its child elements in the custom control, and then layout in the layout file as if you were using LinearLayout .


The second is annoying, it needs to define the child elements in the layout file, to match the child elements in Code onfinishinflate () .


Let me just say how to load the layout file.

First: Define the layout file.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" Horizontal " > <imageview android:id= "@+id/imageview1" android:layout_width= "Wrap_content" android:layout _height= "Wrap_content" android:layout_gravity= "center_vertical" android:paddingbottom= "5dip" Android:        paddingleft= "40dip" android:paddingtop= "5dip" android:src= "@drawable/right_icon"/> <textview        Android:id= "@+id/textview1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_gravity= "center_vertical" android:layout_marginleft= "8dip" android:text= "Theme" Android    : textcolor= "#000000"/> <linearlayout android:layout_width= "100DP" android:layout_height= "Fill_parent" android:orientation= "Horizontal" > <imageview android:id= "@+id/imageview2" android:layout_width= "Wrap_content" Android:layo ut_height= "Wrap_content" android:layout_gravity= "center_vertical" android:paddingbottom= "5dip" Androi        d:paddingleft= "12dip" android:paddingtop= "5dip" android:src= "@drawable/home_icon"/> <imageview        Android:id= "@+id/imageview3" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"        Android:layout_gravity= "center_vertical" android:paddingbottom= "5dip" android:paddingleft= "12dip" android:paddingtop= "5dip" android:src= "@drawable/add_icon"/></linearlayout></linearlayout>

Next, customize a mylinearlayout inheritanceLinearLayout, and load the layout file that you just wrote.

public class Mylinearlayout extends LinearLayout {private ImageView imageview,iv_home,iv_add;private TextView TextView; Public Mylinearlayout (Context context) {super (context);//TODO auto-generated constructor stub}public Mylinearlayout ( Context context, AttributeSet Attrs) {Super (context, attrs);//TODO auto-generated constructor Stublayoutinflater Inflater= (Layoutinflater) Context.getsystemservice (Context.layout_inflater_service); Inflater.inflate ( R.layout.actionbar, this); imageview= (ImageView) Findviewbyid (r.id.imageview1); Iv_home= (ImageView) Findviewbyid ( R.ID.IMAGEVIEW2); iv_add= (ImageView) Findviewbyid (R.ID.IMAGEVIEW3); textview= (TextView) Findviewbyid ( R.ID.TEXTVIEW1);}      /** * Set Picture resource */public void Setimageresource (int resId) {imageview.setimageresource (resId);      }/** * Sets the text displayed */public void Settextviewtext (String text) {textview.settext (text); }  }

Again: Use a defined mylinearlayout control when needed


<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "Horizontal" >    <cn.com.demo.view.mylinearlayout        android:id= "@+id/ll_actionbar"        android:layout_height= "Fill_parent<span style=" Font-family:tahoma, ' Microsoft Yahei ', SimSun; " > "  </span>        android:layout_width=" wrap_content "        android:background=" @drawable/bg "          / ></LinearLayout>






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.