Android Add child views (addview and Setview)

Source: Internet
Author: User
<span id="Label3"></p><p><p>There are two ways to add a view file, one is to define layout in an XML file, and the other is to generate the layouts file dynamically in Java Code.</p></p>Layout defined in XML to be converted to view requires the use of the Layoutinflater class. 1. Construct the XML file 2. Layoutinflater mentioned addview, first of all to understand the Layoutinflater class. The most important feature of this class is the ability to convert the layout of XML representations into VIEW. For the sake of understanding, we can compare it to Findviewbyid (), both of which instantiate an object, and the difference is that Findviewbyid () is instantiated for the specific widget control under the XML layout file, and Layoutinflater is looking for res/ The XML layout file under Layout/is instantiated. (1) Create<pre><pre>Layoutinflater inflater=<span style="color: #000000;"></span>= layoutinflater. <span style="color: #0000ff;"></span> from (Activity.) <span style="color: #0000ff;"></span> this <span style="color: #000000;"></span> = Getlayoutinflater ();</pre></pre><p><p>The three methods are essentially the Same.</p></p><p><p>(2) Inflate ()</p></p><p><p>Use Layoutinflater.inflate () to convert the layout file to View.</p></p><pre><pre><span style="color: #0000ff;">NULL</span>);</pre></pre>3. Add a view file for example, If you define a toast, you can set the view file<pre><pre>Toast.setview (view);</pre></pre>Now give an example of a commonly used toast--let the toast with pictures and text be centered, see the code: where the main file only places a button,xml file.<pre><span style="color: #000000;">package <span style="color: #000000;">com.cn.query; Import Android.app.activity;import android.os.bundle;import android.view.gravity;import Android.view.layoutinflater;import Android.view.view;import Android.widget.button;import Android.widget.ImageView ; Import Android.widget.linearlayout;import Android.widget.textview;import android.widget.Toast; Import com.androidquery.AQuery; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">AQueryTest2 extends Activity {aquery aq</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Aquery (<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">button button;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span><span style="color: #000000;"><span style="color: #000000;">super.oncreate (savedinstancestate); setcontentview (r.layout.test1); aq.id (r.id.button1). visible (). clicked ( </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>,<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Click</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">);} </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Click () {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">dynamically generate layout views-for simple layouts</span></span>Toast toast =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Toast (AQueryTest2.<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); toast.setduration (</span></span><span style="color: #800080;"><span style="color: #800080;"></span> the</span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set the center of Gravity--let The toast appear centered</span></span>Toast.setgravity (gravity.center,<span style="color: #800080;"><span style="color: #800080;">0</span></span>,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">); LinearLayout ll</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>LinearLayout (AQueryTest2.<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); ImageView IV</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>ImageView (AQueryTest2.<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Iv.setimageresource (r.drawable.icon1);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set the image padding so that the TextView appears on the right, avoiding overlap</span></span>Iv.setpadding (<span style="color: #800080;"><span style="color: #800080;">0</span></span>,<span style="color: #800080;"><span style="color: #800080;">0</span></span>,<span style="color: #800080;"><span style="color: #800080;"></span> the</span>,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">layout belongs to viewgroup, you can call Add View method</span></span><span style="color: #000000;"><span style="color: #000000;">Ll.addview (iv); TextView TextView</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>TextView (AQueryTest2.<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); Textview.settext (</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">I am the prompt box to create a message</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #000000;"><span style="color: #000000;">ll.addview (textview); toast.setview (ll); toast.show ();} </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Click2 () {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">dynamically generate layout views-for complex UI layouts</span></span>Toast toast =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Toast (AQueryTest2.<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span><span style="color: #000000;"><span style="color: #000000;">); toast.setduration (</span></span><span style="color: #800080;"><span style="color: #800080;"></span> the</span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Set center of gravity</span></span>Toast.setgravity (gravity.center,<span style="color: #800080;"><span style="color: #800080;">0</span></span>,<span style="color: #800080;"><span style="color: #800080;">0</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Create Inflater</span></span>Layoutinflater Inflater =<span style="color: #000000;"><span style="color: #000000;">Getlayoutinflater ();</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">transform layout into view with the Inflate method</span></span>View view = Inflater.inflate (r.layout.toast,<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">);</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">set view--toast inherit from widget, not container, only Call Set View method</span></span><span style="color: #000000;"><span style="color: #000000;">toast.setview (view); toast.show ();}}</span></span></pre><span style="color: #000000;"><span style="color: #000000;">Click</span></span>() method is a dynamically generated layout, and it's not much to Say. Note that Ll.addview (iv) is addview, because LinearLayout inherits from viewgroup, so it is a container, and the container adds a view with the AddView (). Click2 () method when you define layout in an XML file, The layout is then converted to view by invoking the inflate method through the instantiation object of the Layoutinflater class Inflater. Note Toast.setview (), toast is a widget, not a container, you can only set the view with Setview (). Layout file used in the Click2 () method:<br>Toast.xml<pre><?xml version=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">1.0</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>encoding=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Utf-8</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>? ><linearlayout xmlns:android=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">http://schemas.android.com/apk/res/android</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">match_parent</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">match_parent</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">android:gravity</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Center_vertical|center_horizontal</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">android:orientation</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">Horizontal</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>> <<span style="color: #000000;"><span style="color: #000000;">ImageView Android:id</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">@+id/imageview3</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">fill_parent</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">wrap_content</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Android:paddingbottom</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">0px</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Android:paddingleft</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">0px</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Android:paddingright</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">5px</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Android:paddingtop</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">0px</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">android:src</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">@drawable/icon1</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>/> <<span style="color: #000000;"><span style="color: #000000;">TextView Android:id</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">@+id/textview3</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">wrap_content</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">wrap_content</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #000000;"><span style="color: #000000;">Android:text</span></span>=<span style="color: #800000;"><span style="color: #800000;">"</span></span><span style="color: #800000;"><span style="color: #800000;">message Prompt</span></span><span style="color: #800000;"><span style="color: #800000;">"</span></span>/> </LinearLayout></pre>In addition to the above also use the Android Aquery lightweight plugin. You need to import the appropriate Package. Effect:<p><p>Android Add child views (addview and Setview)</p></p></span>
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.