First line of code Android-------Chapter two controls section

Source: Internet
Author: User

One, the control

1. Size

MATCH_PARENR: Same size as parent layout

Fill_parent: Same as Match_parent

Wrap_content: The size of the control is just inside the content

2, TextView display a paragraph of text on the interface

Android:text = "text Displayed"

Android:gravity: "Text alignment"//selectable values are top, bottom, right, center, you can specify multiple values with |

Android:textsize: Text Size

Android:textcolor: Text color

3. Button buttons

Android:text: Display text

Android:textallcaps: Case conversion disabled, convert all to uppercase by default

Register Listener

Button.setonclicklistener (New View.onclicklistener () {

public void OnClick (View v) {}

})

4. EditText input and edit content

Android:hint= "Prompt for input"

Android:maxlines= "Maximum number of lines entered"

5, ImageView display pictures

android:src= "@drawable/pictures"

Change Picture content

Image.setimageresource ();

6. ProgressBar Progress bar

android:visibility= "visible", visibility visible invisible hidden gone completely invisible and does not occupy space

Modify Style

Style: "? Android:attr/progressbarstylehorizontal"//Horizontal progress bar

Setting visibility

Setvisibility (view.visiable);//view.invisible,view.gone;

7, Alterdialog

Popup dialog box

8, ProgressDialog

Two or four types of layouts

1. LinearLayout Linear layout

Android:orientation direction

android:layout_gravity= position in Layout

android:layout_weight= Control Size Scale

2, Relativelayout relative layout

3. Framelayout Frame Layout

4. Percent layout

Third, custom controls

1. Create a layout

New Layout.xml File

2. Introduction of layout file in Mainlayout.xml file

<include layout= "Layout.xml" >

3. Writing Events

Layoutinflater.from (Context). Inflater (R.layout.xxx,this);

4. Or introduce a custom control event file in the layout file

<packagename.activityfile

Android:layout_width= ""

Android:layout_height= ""/>

Iv. ListView for displaying large amounts of data

1, write layout files and display layout files

<listview

Andriod:id=

Andrioid:layout_width= ""

Andriod:layout_height= "" >

2, the new display class

Class fruit{}

3. Rewrite Adapter Adapter

public class Fruitadapter extends arrayadapter<fruit>{

private int resourceId;

Public Fruitadapter (Context context,int textviewresourceid,list<fruit>objects) {

Super (context,textviewresource,objects);

ResourceId = Textviewresourceid;

}

Public View getView (int position,view convertview,viewgroup parent) {

Fruit Fruit = GetItem (position);

View view = Layoutinflater.from (GetContext ()). Inflate (resourceid,parent,flase);

TextView TextView =view.findviewbyid (R.id.textview);

return view;

}

4, modify the Mainactivity

Fruitadapter fruitadapter = new Fruitadapter (mainactivity.this,r.layout.fruit_item,fruitlist);//display context, layout layouts, List of displayed data

ListView ListView = Findviewbyid (R.id.list_view);

Listview.setadapter (adapter);

5. Optimization

1.converView, cache the previously loaded layout so that it can be reused later

if (Convertview = = null) {

View = Layoutinflater.from (GetContext ()). Inflater (Resourceid,parent,false);

}else{

view = Convertview;

}

2. Create the Viewholder inner class to cache the control instance to get only one instance of the layout

}

V. Recyclerview optimization of the ListView

1. Introduction

Add in dependence in Build.gradle

Implement ' com.android.support:recyclerview-v7:24.2.1 '

Then sync now synchronizes

2. Use in Layout file

<android.support.v7.widget.recyclerview

Android:id= "@+id/"

Android:layout_wight= ""

Android:layout_height= ""/>

3. Create an Adapter

public class Fruitadapter extends recyclerview.adapter<fruitadapter.viewholder>{

Static class Viewholder extneds recycler.viewholder{}; Viewholder Inner class

Public Fruitadapter () {}; constructor function

Public Viewholder Oncreateviewholder (viewgroup parent,int position) {

View view = Layoutinflate.from (Parent.getcontext ()). Inflate (R.layour.fruit_layout,parent,false);

Viewholder Viewholder = new Viewholder (view);

return viewholder;

}

Public Viod Onbindviewholder (viewholder holder, int position) {

Fruit Fruit = furitlist.get (position);

Holder.fruitImage.setImageREsource (Fruit.getimageid ());

Holder.fruitname.setText (Fruit.getname ());

}

public int GetItemCount () {

return Fruitlist.size ();

}

}

4, modify the Mainactivity

Recyclerview Recyclerview = (recyclerview) Findviewbyid (R.layout.id.recyclerview);

Linerlayoutmanager layoutmanage = new Linerlayoutmanager (this);

Recycleview.setlayoutmanager (LayoutManager);

Fruitadapter adapter = new Fruitadapter (fruitlist);

Recyclerview.setadapter (adapter);

First line of code Android-------Chapter two controls section

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.