Android & lt; uses nested layout to implement the calculator interface from scratch. 17. & gt;, android nesting

Source: Internet
Author: User

Learning android from scratch <using nested layout to implement the calculator interface. 17 ..>, android nesting

The so-called nested layout is to nest 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 we will use the nested layout to implement the UI effect of a simple calculator.

The following figure shows the basic concept.



Use the Xml layout file to set the calculator Effect

<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="vertical" >    <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_width="match_parent"            android:layout_height="fill_parent"            android:layout_weight="0.2" >            <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="@android: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            android: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/darker_gray"                android:text="7" />            <Button                android:id="@+id/eight_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/darker_gray"                android:text="8" />            <Button                android:id="@+id/nine_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/darker_gray"                android:text="9" />            <Button                android:id="@+id/product_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: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/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="25sp"                android:background="@android:color/darker_gray"                android:text="4" />            <Button                android:id="@+id/five_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/darker_gray"                android:text="5" />            <Button                android:id="@+id/six_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/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: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/one_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/darker_gray"                android:text="1" />            <Button                android:id="@+id/two_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/darker_gray"                android:text="2" />            <Button                android:id="@+id/three_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/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="25sp"    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="@android:color/darker_gray"                android:text="." />            <Button                android:id="@+id/zero_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="25sp"                android:background="@android:color/darker_gray"                android:text="0" />                        <Button                android:id="@+id/equal_button"                android:layout_height="fill_parent"                android: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>


The following is a simulator.


The above code is used repeatedly

<span style="font-size:18px;"> android:layout_weight=""</span>
To set a layout, you must understand the concept of weight. android: layout_weight


Next forecast:

ScrollView




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.