The "Android" uses the relative layout layout to update the software's style as the activity of the theme dialog, using the Layout_weight property to divide the row of the table layout

Source: Internet
Author: User

In Android layouts, you can also use a div layout with a Web page, specifying the width as a percentage. It is also possible to use style as the activity of the Theme dialog box to complete modal windows like vb,c# or the absolute layout in a Web page. For example, the layout shown is common in all types of Android applications today. Also, set up an activity that updates the software at the beginning of the Android program. So how do we do that?


1, first of all, first to Res\values\string.xml in the layout of the various components of the font, of course, you can do the Android project side Add. When naming, there are rules that indicate which activity the font is in, and then indicate which component of the font it is. I would also like to put fonts directly in the various layout files, but then eclipse will warn, but also not conducive to future internationalization software needs. Can be seen from this file, the program has two activity, one is mainactivity, there are 6 buttons, one is updateactivity is the Update Reminder dialog box.

<?xml version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_ Name > Layout </string> <string name= "updateactivity_name" > Update reminder </string> <string name= "Action_ Settings ">Settings</string> <string name=" Hello_world ">hello world!</string> <string name=" Mainactivity_button1 "> Function 1</string> <string name=" Mainactivity_button2 "> Function 2</string> < String name= "Mainactivity_button3" > Function 3</string> <string name= "Mainactivity_button4" > Function 4</string > <string name= "mainactivity_button5" > Function 5</string> <string name= "Mainactivity_button6" > function 6 </string><string name= "Updateactivity_textview1" > Found new version, is it updated? </string><string name= "Updateactivity_button1" > Update </string><string name= "UpdateActivity_ Button2 "> Cancel </string></resources> 
2, after the first to mainactivity layout, modify the Res\layout\activity_main.xml file, such as the following code:

<tablelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:gravi ty= "center" > <tablerow android:layout_weight= "1" > <linearlayout android:layout_width= "0                Dip "android:layout_height=" match_parent "android:layout_weight=" 1 "> <button Android:id= "@+id/button1" android:layout_width= "0dip" android:layout_height= "match            _parent "android:layout_weight=" 1 "android:text=" @string/mainactivity_button1 "/> <button android:id= "@+id/button2" android:layout_width= "0dip" Android: layout_height= "Match_parent" android:layout_weight= "1" android:text= "@string/mainactivity_bu       Tton2 "/> </LinearLayout> <linearlayout android:layout_width= "0dip" android:layout_height= "Match_parent" Androi d:layout_weight= "1" > <button android:id= "@+id/button3" Android:layout_widt H= "0dip" android:layout_height= "Match_parent" android:layout_weight= "1" Andro id:text= "@string/mainactivity_button3"/> </LinearLayout> </TableRow> <tablerow android Oid:layout_width= "Match_parent" android:layout_weight= "1" > <button android:id= "@+id/button 4 "android:layout_width=" 0dip "android:layout_height=" Match_parent "android:layout_weight            = "1" android:text= "@string/mainactivity_button4"/> <button android:id= "@+id/button5"             Android:layout_width= "0dip" android:layout_height= "Match_parent" android:layout_weight= "1" Android:text= "@string/mainactivity_button5"/> </TableRow> <tablerow android:layout_width= "Match_parent" android:layout_weight= "1" > <button android:id= "@+id/button6" android:layout_width= " Match_parent "android:layout_height=" Match_parent "android:layout_weight=" 1 "android:text = "@string/mainactivity_button6"/> </TableRow></TableLayout>
Here the layout ideas are as follows:


Set up a table layout full of screens, the table layout is not occupied by default, and you can set its match_parent to no use.

Just specify its android:layout_weight= "1" for each row.

The 1 here is not the meaning of 100%. Is the weight that it occupies. If there are two and two components above, the entire row is divided according to this weight

For example, in the first line of two linear layout, are set android:layout_width= "0dip" after the android:layout_weight= "1". The two layouts occupy the width of the first row of the table layout, which is the 1/(=50%) of this line. That is, if you want the component or layout to split the line, you can set the weights for each component after setting the Android:layout_width= "0dip" and then using Android:layout_weight.

The first line, in fact, can also not linear layout, in three buttons are set android:layout_width= "0dip" after the button 1 android:layout_weight= "1", Button 2 Android:layout_weight = "1", Button 3 android:layout_weight= "2" can make the above effect.

And then two of the same.

3, then, as "Android" between multiple activity using the bundle transfer value (click Open link), create a new activity class named Updateactivity.java, its layout file is Activity_ Update.xml, Android rules layout files can only be a-z,0-9,_ can not have other symbols, can not have uppercase, very annoying. The main fear there are other symbols, in the R file you can not register. The activity class is done in the project package, and the corresponding layout file is res\layout in the Register the activity with the application node of Androidmanifest.xml, specify the implementation class, and specify its subject as the theme dialog so that the activity opens without overwriting the entire screen, opening the modal window, the entire androidmanifest . XML is modified as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/        Android "package=" Com.example.layouttest "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application android:allowbackup=" "True" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/ap Ptheme "> <activity android:name=" com.example.layouttest.MainActivity "android:label=" @  String/app_name "> <intent-filter> <action android:name=" Android.intent.action.MAIN "         /> <category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <!--Register Updateactivity---<activity android:name= "com.example. Layouttest. Updateactivity "ANdroid:label= "@string/updateactivity_name" Android:theme= "@android: Style/theme.dialog" > </activit Y> </application></manifest>
4, in Activity_update.xml with relative layout for update remind this updateactivity Cloth Good Bureau, code as follows: Set the font of each component, height and width are wrapped good content, you can. After that, the Button2 is specified by Layout_alignright to the right end of its TextView1, and Button2 is placed under TextView1 by Layout_below. Where TextView1 is a reminder of the updated text, Button2 is the Cancel button. The Button1 is then placed below the TextView1, but placed on the left side of the Button2. In this way, the updateactivity layout is reminded of the update success.

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "match_parent "android:layout_height=" match_parent "> <textview android:id=" @+id/textview1 "Android:layout_wi        Dth= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/updateactivity_textview1"         Android:textsize= "24sp"/> <button android:id= "@+id/button2" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignright= "@+id/textview1" Android:layout_below        = "@+id/textview1" android:text= "@string/updateactivity_button2" android:textsize= "18sp"/> <button        Android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_below= "@+id/textview1" android:layout_toleftof= "@+id/button2" android:text= "@string/UPDA Teactivity_button1 "AndroidOid:textsize= "18SP"/></relativelayout> 
5, modify the Updateactivity.java code, for the "Cancel" button to specify the event, is to close the activity, as follows:

Package Com.example.layouttest;import Android.app.activity;import Android.os.bundle;import Android.widget.Button; Import Android.view.view;import Android.view.view.onclicklistener;public class Updateactivity extends Activity { Private Button button2;protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_update); button2 = (Button) Findviewbyid (r.id.button2); Button2.setonclicklistener (New Onclicklistener () {//Button2 add click event @overridepublic void OnClick (View v) {finish ();//close this activity  });}}
6, the last modification Mainactivity.java code, just a sentence, the program starts, the update reminds the activity of the updateactivity. In general, is to set a version number on the server, in the app set a version number, start to determine whether the same, and then pop up this dialog box.

Package Com.example.layouttest;import Android.os.bundle;import Android.app.activity;import android.content.Intent; Import Android.view.menu;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Intent Intent = new Intent (Mainactivity.this, Updateactivity.class); startactivity (Intent);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
This concludes the entire app development.

The "Android" uses the relative layout layout to update the software's style as the activity of the theme dialog, using the Layout_weight property to divide the row of the table layout

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.