Learn from scratch android< using nested layouts to implement the calculator interface. 17 .>

Source: Internet
Author: User

The so-called nested layout is the nesting of multiple layout files in a file

<span style= "FONT-SIZE:18PX;" >  <linearlayout        android:layout_width= "match_parent"        android:layout_height= "Fill_parent        " android:orientation= "vertical" ><framelayout android:layout_width= "match_parent"        android:layout_height = "Fill_parent"        android:orientation= "vertical" >..................</framelayout ></linearlayout></span >

Next, use nested layouts to implement the UI effect of a simple calculator

Here's the basic idea map.



Set up a calculator effect using an XML layout file

<span style= "FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/ Res/android "android:layout_width=" match_parent "android:layout_height=" match_parent "android:orientation=" vertic        Al "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Fill_parent " android:layout_weight= "3" android:orientation= "vertical" > <textview android:id= "@+id/text2 "Android:layout_width=" match_parent "android:layout_height=" Fill_parent "Android:layout_            margintop= "0DP" android:layout_weight= "1" android:padding= "0DP"/> <textview            Android:id= "@+id/text1" android:layout_width= "match_parent" android:layout_height= "Fill_parent" android:layout_margintop= "0DP" android:layout_weight= "2" android:padding= "0DP"/> < /linearlayout> <tablelayout android:layout_width= "match_parent" android:layout_height= "Fill_parent" android:layout_weight= "1" android:orientation= "vertical" > <tablerow android:layout_wi Dth= "Match_parent" android:layout_height= "fill_parent" android:layout_weight= "0.2" > & Lt                Button android:id= "@+id/left_bracket_button" android:layout_height= "Fill_parent" Android:layout_marginbottom= "1DP" android:layout_marginleft= "1DP" android:layout_weight= "0.25" android:background= "#333333" android:text= "(" android:textcolor= "@andr Oid:color/white "android:textsize=" 25sp "/> <button android:id=" @+id/right_                Bracket_button "android:layout_height=" fill_parent "android:layout_marginbottom=" 1DP " Android:layout_marginleft= "1DP" android:layout_weight= "0.25" android:background= "#333333"            android:text= ")" Android:textcolor= "@android: Color/white" android:textsize= "25sp"/>                <button android:id= "@+id/divisor_button" android:layout_height= "Fill_parent" Android:layout_marginbottom= "1DP" android:layout_marginleft= "1DP" Android:layout_ weight= "0.25" android:background= "#333333" android:text= "÷" Android:textcolor = "@android: Color/white" android:textsize= "25sp"/> <button android:id= "@+id                /backspace_button "android:layout_height=" fill_parent "android:layout_marginbottom=" 1DP " android:layout_marginleft= "1DP" android:layout_weight= "0.25" android:background=               "#ffa500" android:text= "C" android:textsize= "25sp"/> </TableRow> <tablerow and Roid:layout_width= "Match_parent" android:layout_height= "fill_parent" android:layout_weight= "0.2"              ;                <button android:id= "@+id/seven_button" android:layout_height= "Fill_parent" Android:layout_width= "Fill_parent" android:layout_weight= "0.25" android:layout_marginleft= " 1DP "android:layout_marginbottom=" 1DP "android:textsize=" 25sp "android:background=" @android: Color/dar                Ker_gray "android:text=" 7 "/> <button android:id=" @+id/eight_button " android:layout_height= "Fill_parent" android:layout_width= "Fill_parent" Android:layo ut_weight= "0.25" android:layout_marginleft= "1DP" android:layout_marginbottom= "1DP" android:textsize= "    25SP "            Android:background= "@android: Color/darker_gray" android:text= "8"/> <button Android:id= "@+id/nine_button" android:layout_height= "Fill_parent" android:layou T_width= "Fill_parent" android:layout_weight= "0.25" android:layout_marginleft= "1DP" Androi                D:layout_marginbottom= "1DP" android:textsize= "25sp" android:background= "@android: Color/darker_gray" android:text= "9"/> <button android:id= "@+id/product_button" and roid:layout_height= "Fill_parent" android:layout_width= "fill_parent" android:layout_weight= "0 . "Android:layout_marginleft=" 1DP "android:layout_marginbottom=" 1DP "Android:background = "#333333" android:textsize= "25sp" android:textcolor= "@android: Color/white" android:text= "X"/> </tablerow> <tablerow android:layout_width= "match_parent" android:layout_height= "Fill_paren                T "android:layout_weight=" 0.2 "> <button android:id=" @+id/four_button " android:layout_height= "Fill_parent" android:layout_width= "Fill_parent" android:layout _weight= "0.25" android:layout_marginleft= "1DP" android:layout_marginbottom= "1DP" android:textsize= "25 SP "android:background=" @android: Color/darker_gray "android:text=" 4 "/> <bu Tton android:id= "@+id/five_button" android:layout_height= "Fill_parent" Androi    D:layout_width= "Fill_parent" android:layout_weight= "0.25" android:layout_marginleft= "1DP" Android:layout_marginbottom= "1DP" android:textsize= "25sp" android:background= "@android: Color/darker_gra Y "Android:text= "5"/> <button android:id= "@+id/six_button" android:layout_height= "f Ill_parent "android:layout_width=" fill_parent "android:layout_weight=" 0.25 "a ndroid:layout_marginleft= "1DP" android:layout_marginbottom= "1DP" android:textsize= "25SP" Android:back Ground= "@android: Color/darker_gray" android:text= "6"/> <button android:id=                "@+id/minus_button" android:layout_height= "fill_parent" android:layout_width= "Fill_parent" android:layout_weight= "0.25" android:layout_marginleft= "1DP" android:layout_marginbottom= " 1DP "android:background=" #333333 "android:textsize=" 25sp "android:textcolor=" @android: Col Or/white "android:text="-"/> </TableRow> <tablerow Android:layout_wid       Th= "Match_parent"     android:layout_height= "Fill_parent" android:layout_weight= "0.2" > <button Android:id= "@+id/one_button" android:layout_height= "fill_parent" android:layout_width= "fill _parent "android:layout_weight=" 0.25 "android:layout_marginleft=" 1DP "Android:layout_marg Inbottom= "1DP" android:textsize= "25sp" android:background= "@android: Color/darker_gray" an droid:text= "1"/> <button android:id= "@+id/two_button" Android:layout_heigh                t= "Fill_parent" android:layout_width= "fill_parent" android:layout_weight= "0.25" android:layout_marginleft= "1DP" android:layout_marginbottom= "1DP" android:textsize= "25sp" Android: Background= "@android: Color/darker_gray" android:text= "2"/> <button Android : id= "@+id/three_button"               android:layout_height= "Fill_parent" android:layout_width= "Fill_parent" Androi d:layout_weight= "0.25" android:layout_marginleft= "1DP" android:layout_marginbottom= "1DP" Android:text            Size= "25SP" android:background= "@android: Color/darker_gray" android:text= "3"/>                <button android:id= "@+id/plus_button" android:layout_height= "Fill_parent" Android:layout_width= "Fill_parent" android:layout_weight= "0.25" android:layout_marginleft= "1DP" android:layout_marginbottom= "1DP" android:background= "#333333" android:textsize= "25 SP "android:textcolor=" @android: Color/white "android:text=" + "/> </TableRow> < TableRow android:layout_width= "match_parent" android:layout_height= "Fill_parent" Android: layout_weight= "0.2" >            <button android:id= "@+id/point_button" android:layout_height= "Fill_parent" Android:layout_width= "wrap_content" android:layout_weight= "0.5" android:layout_ marginleft= "1DP" android:layout_marginbottom= "1DP" android:textsize= "40sp" android:background= "@andro Id:color/darker_gray "android:text=". "/> <button android:id=" @+id/zero_but                Ton "android:layout_height=" Fill_parent "android:layout_width=" Wrap_content " android:layout_weight= "0.5" android:layout_marginleft= "1DP" android:layout_marginbottom= "1DP" Android                        : textsize= "25sp" android:background= "@android: Color/darker_gray" android:text= "0"/> <button android:id= "@+id/equal_button" android:layout_height= "Fill_par Ent "AndroidOid:layout_width= "Wrap_content" android:layout_weight= "3.1" android:layout_marginleft= "1DP"                Android:layout_marginbottom= "1DP" android:textsize= "25sp" android:background= "#00CC00" android:text= "="/> </TableRow> </TableLayout></LinearLayout></span>


here is the simulator


The above code is used over and over again.

<span style= "FONT-SIZE:18PX;" > android:layout_weight= "" </span>
To set up a layout you must understand the concept of weights android:layout_weight the true meaning of


The next section predicts:

Scrolling View ScrollView

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.