Android custom widget (1)

Source: Internet
Author: User

Android custom widget (1)

I recently attended a sharing meeting in Jinan. I felt deeply touched by the sharing spirit of the great gods. I would like to share some good things with you.

I cannot be a code farmer who only moves bricks to become a real programmer. Of course, I am a Yuan programmer. There are three types of programmers: Junior programmers, intermediate programmers, and senior programmers, I can't always be a newbie, and that's too hard to pursue. For the sake of my dream, I started to learn custom controls.

This blog introduces custom composite controls from the very beginning. In my opinion, it is the easiest to use composite controls in a custom space. Here we will illustrate the most common combinations of images and text.

First, go to the interface:


This interface is still very common. Many app home pages use this interface. Of course, you can set the image text below.

The following is the main code for custom composite controls:

The first step is to inherit the custom control from LinearLayout.

package com.sdufe.thea.guo.view;import android.content.Context;import android.graphics.drawable.Drawable;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import com.sdufe.thea.guo.R;public class CustomView extends LinearLayout {private ImageView mImageView;private TextView mTextView;Context context;public CustomView(Context context) {super(context,null);}public CustomView(Context context, AttributeSet attrs) {super(context, attrs);this.context=context;View view=LayoutInflater.from(context).inflate(R.layout.custom_view, this,true);mTextView=(TextView) view.findViewById(R.id.custom_textview);mImageView=(ImageView) view.findViewById(R.id.custom_imageview);}public void setText(String text){mTextView.setText(text);}public void setTextColor(int color){mTextView.setTextColor(color);}public void setTextSize(float size){mTextView.setTextSize(size);}public void setImagViewResouce(Drawable drawable){mImageView.setImageDrawable(drawable);}}

Then the layout file of the custom control

 
         
          
  
 

Finally, the use of custom controls

     
          
           
           
           
       
  
 

Package com. sdufe. thea. guo; import com. sdufe. thea. guo. view. customView; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. widget. toast; public class MainActivity extends Activity {private CustomView mCustomView; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. activity_main); initView ();} private void initView () {mCustomView = (CustomView) findViewById (R. id. custom); mCustomView. setText ("I am a bad guy"); mCustomView. setTextColor (getResources (). getColor (R. color. text_color); mCustomView. setImagViewResouce (getResources (). getDrawable (R. drawable. phone); mCustomView. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (getApplicationContext (), "don't click me", Toast. LENGTH_LONG ). show ();}});}}

The above is all the code. Here we will talk about my problems. I usually use LayoutInflater when filling the layout. from (context ). inflate (resource, root), but it cannot be displayed when you define it. In the spirit of programmers, you can check the source code and change it to LayoutInflater. from (context ). inflate (R. layout. custom_view, this, true); that's it. Well, it is displayed successfully. I'm very happy. By the way, I checked the source code.

Generally, LayoutInflater. from (context ). inflate (resource, root). The first parameter is the layout id to be loaded, and the second parameter is to nest a parent layout to the outside of the layout, if you do not need to directly pass null, but here you need to use its attributes, obviously not satisfied, then you can only use three parameters, then let's talk about the LayoutInflater of the three parameters. from (context ). inflate (R. layout. custom_view, this, true); the first parameter is the loaded layout id, and the second parameter is to nest a parent layout to the outside of the layout, the third official website says attachToRoot Whether the inflated hierarchy shoshould be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML. Chinese means attachToRoot should be appended to the root parameter regardless of the inflation level? If it is false, the root is only used for the XML Root View in the correct subclass of the created LayoutParams.

public View inflate(int resource, ViewGroup root, boolean attachToRoot) {        if (DEBUG) System.out.println("INFLATING from resource: " + resource);        XmlResourceParser parser = getContext().getResources().getLayout(resource);        try {            return inflate(parser, root, attachToRoot);        } finally {            parser.close();        }    }
When you continue to trace the fifth line of code, you will know that the essence of the code is to use the Pull parsing in XML. Here we will put it for the moment, just to illustrate the three parameters.

1. When the root is empty, attactRoot is invalid.

2. If the root is not empty, if the value of attachRoot is TRUE, the layout file's outer root will be loaded.

3. When root is not empty and attachroot is FALSE, layout files at the outermost layer will not be loaded.

OK, let's first introduce this. This blog is mainly used to implement custom from Java code, and the next article intends to implement custom composite controls from attributes.


Code: http://download.csdn.net/detail/elinavampire/8141963

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.