<span id="Label3"></p><p><p><span style="white-space:pre"></span>Dynamically add a View.</p></p><p><p><span style="white-space:pre"></span>first, figure out which view (sub-view) to add to which view (the parent view), and secondly, How to set the properties of the child view; these two problems are easy to understand.</p></p><p><p><span style="white-space:pre"></span>For example: to add a button to a linearlayout, the child view is a button, and the parent view is Linearlayout. The properties of the child view are <span style="background-color:rgb(240,240,240)">set by layoutparams, and note that it is LinearLayout</span><span style="background-color:rgb(240,240,240)">. layoutparams, because the height of the child view, the width of these are for the parent view, to tell the parent view itself to occupy much space, so it is <span style="background-color:rgb(240,240,240)">LinearLayout (the original will always use the child view of the <span style="background-color:rgb(240,240,240)">layoutparams to set, Error</span> )</span> </span></p></p><p><p><span style="white-space:pre"></span></p></p><pre name="code" class="java"><pre name="code" class="java">public class MyActivity extends Activity { private Context mcontext; Private LinearLayout mlinearlayout; Private Linearlayout.layoutparams mlayoutparams; @Override public void onCreate (Bundle Savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Mcontext = this; Mlinearlayout = (linearlayout) Findviewbyid (r.id.parent_view); Mlayoutparams = new Linearlayout.layoutparams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_ CONTENT); Button button = New button (mcontext); Button.settext ("add button"); Mlinearlayout.addview (button, mlayoutparams);} }</pre></pre><br><p><p></p></p><p><p><span style="white-space:pre"></span></p></p><pre name="code" class="html"><pre name="code" class="html"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <linearlayout android:id= "@+id/parent_view" android:orientation= "vertical" android:layout_width= "match_parent" android:layout_height= "wrap_content"/></linearlayout></pre></pre><br><br><p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android Dynamic add view</p></p></span>
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