Android Customize view with Typedarray configuration style properties in detail _android

Source: Internet
Author: User

Android Custom View with Typedarray configuration style properties Detail

In order to improve reusability and extensibility when customizing view, you can add the configuration of style attributes for custom view, such as customizing picture resources, Text size, control properties, and so on, which requires the use of the Typedarray class. The following example records the simple use of this class with a custom clickable expansion and contraction textview.

First up Effect chart:

Click later for


Re-pasting code:

1. Custom view class;

/** * @title Expandtextview * @description extensible TextView can be defined by setting Expandtextviewstyle to customize the number of lines to expand the picture, to close the picture, and to display a minimum of rows/public cl Ass Expandtextview extends LinearLayout implements Onclicklistener {/** * default minimum number of rows to display * * Private int Defaultm 
  InLines; 
  /** * Whether to expand * * Private Boolean mcollapsed = true; 
 
  /** * Re-layout * * Private Boolean mrelayout = false; 
  Private View Expandview; 
  Private TextView Expandtext; 
  Private ImageView expandimg; 
  Private drawable mexpanddrawable; 
 
  Private drawable mcollapsedrawable; 
  Public Expandtextview {This (context, NULL); 
    Public Expandtextview (context, AttributeSet attrs) {Super (context, attrs); 
  Init (attrs); } private void Init (AttributeSet attrs) {Expandview = Layoutinflater.from (GetContext ()). Inflate (R.lay 
    Out.pt__expand_textview, NULL); 
    Expandtext = (TextView) Expandview.findviewbyid (R.id.expand_text); Expandtext.setonclicklisteNER (this); 
    Expandimg = (ImageView) Expandview.findviewbyid (r.id.expand_img); 
 
    Expandimg.setonclicklistener (this); 
    TypedArray a = GetContext (). Obtainstyledattributes (Attrs, R.styleable.expandtextviewstyle); Custom Picture Resource mexpanddrawable = Getresources (). Getdrawable (A.getresourceid (r.styleable.expandtextviewstyle_exp 
    And, R.drawable.pt__ic_expand)); 
    Expandimg.setbackgrounddrawable (mexpanddrawable); 
            mcollapsedrawable = Getresources (). Getdrawable (A.getresourceid (R.styleable.expandtextviewstyle_collapse, 
    R.drawable.pt__ic_collapse)); 
    Custom minimum row number Defaultminlines = A.getint (R.styleable.expandtextviewstyle_default_min_lines, 2); 
 
    A.recycle (); 
    Linearlayout.layoutparams params = new Layoutparams (layoutparams.fill_parent, layoutparams.wrap_content); 
    params.gravity = Gravity.center; 
  AddView (Expandview, params); } @Override protected void onmeasure (intWidthmeasurespec, int heightmeasurespec) {if (!mrelayout) {super.onmeasure (Widthmeasurespec, Heightmeasuresp 
      EC); 
    Return 
    } mrelayout = false; 
    Expandtext.setmaxlines (Integer.max_value); 
    Expandimg.setvisibility (View.gone); 
    Super.onmeasure (Widthmeasurespec, Heightmeasurespec); 
    if (Expandtext.getlinecount () <= defaultminlines) {return; 
    } if (mcollapsed) {expandtext.setmaxlines (defaultminlines); 
    } expandimg.setvisibility (view.visible); 
  Super.onmeasure (Widthmeasurespec, Heightmeasurespec); 
    public void SetText (charsequence text) {mrelayout = true; 
  Expandtext.settext (text); 
  public void SetText (int resid) {This.settext (GetContext (). getString (Resid)); 
    @Override public void OnClick (view view) {if (expandimg.getvisibility ()!= view.visible) {return; 
    } mcollapsed =!mcollapsed; Expandimg.setbackgrounddrawable (mcollapsed? mexpa)nddrawable:mcollapsedrawable); 
  Expandtext. Setmaxlines (mcollapsed DefaultMinLines:Integer.MAX_VALUE);  } 
}

2. Define style attributes in the Attrs.xml file added under Res/values;

<resources> 
 
  <!--****************************** extensible Expandtextview Style *******************************- -> 
  <declare-styleable name= "Expandtextviewstyle" > 
 
    <!--expand the picture--> <attr name= 
    "expand" format= "Reference"/> 
    <!--close picture--> 
    <attr name= "collapse" format= "reference"/> 
    <!-- Minimum row number--> 
    <attr name= "default_min_lines" format= "integer"/> 
  </declare-styleable> 
 

3. In the res/values under the Style.xml file to define the style, you can replace the picture resources;

<!--extensible Expandtextview style--> 
  <style name= "Expandtextviewstyle" > 
    <item name= "expand" >@ drawable/pt__ic_expand</item> 
    <item name= "collapse" > @drawable/pt__ic_collapse</item> 
    <item name= "Default_min_lines" >3</item> 
  

4. Layout documents;

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" 
  xmlns:custom= "http:// Schemas.android.com/apk/res/com.example.typedarraytest " 
  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.typedarraytest.ExpandTextView 
    android:id= "@+id/expand_text_view" 
    style= "@style Expandtextviewstyle " 
    android:layout_width=" fill_parent " 
    android:layout_height=" Wrap_content 
    " Android:layout_margin= "8DP" 
    custom:default_min_lines= "2"/> 
 

Below is a brief description of the implementation steps:

1. First, define the Attrs.xml document;

2. To get the defined style properties in the custom view class, the following lines of code are key:

TypedArray a = GetContext (). Obtainstyledattributes (Attrs, 
    r.styleable.expandtextviewstyle); 
Custom Picture resource 
mexpanddrawable = Getresources (). Getdrawable ( 
    A.getresourceid (r.styleable.expandtextviewstyle_ Expand, 
        R.drawable.pt__ic_expand)); 
Expandimg.setbackgrounddrawable (mexpanddrawable); 
mcollapsedrawable = Getresources (). Getdrawable ( 
    A.getresourceid (R.styleable.expandtextviewstyle_collapse, 
        r.drawable.pt__ic_collapse)); 
Custom minimum row number 
Defaultminlines = A.getint ( 
    r.styleable.expandtextviewstyle_default_min_lines, 2); 

3. You can either define the style directly in Style.xml and then use it, or you can configure the properties in the layout file:

Custom:default_min_lines= "2"

To use the above properties, you need to add the following properties to the root node of the layout file:

xmlns:custom= http://schemas.android.com/apk/res/com.example.typedarraytest

Format: xmlns: custom keyword (for using properties in controls, with Android) =http://schemas.android.com/apk/res/package name

Thank you for reading, I hope to help you, thank you for your support for this site!

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.