【Android】使用LinearLayout实现计算器效果

来源:互联网 发布:mp3 编辑软件 mac 编辑:程序博客网 时间:2024/06/04 18:06


Ubuntu下截屏花了好长时间,最后设置的快捷键为Crtl+Alt+A


这个效果实际上是这么做的。

首先整个页面分为6个水平线性布局,最后一个占两行。外面那个大布局是垂直排列。

难点在于最后一个,将其分为两个水平线性布局,第一个又包含了两个布局(垂直排列),第二个就是等号按钮。


代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#FFFFFF"    android:orientation="vertical" >    <!-- 布局可以嵌套布局 -->    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <EditText            android:id="@+id/msg"            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="mc" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="m+" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="m-" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="mr" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="C" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="+/-" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="/" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="*" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="7" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="8" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="9" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="-" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="4" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="5" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="6" />        <Button            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_weight="1"            android:text="+" />    </LinearLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="3"            android:orientation="vertical" >            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:orientation="horizontal" >                <Button                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:layout_weight="1"                    android:text="1" />                <Button                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:layout_weight="1"                    android:text="2" />                <Button                    android:layout_width="match_parent"                    android:layout_height="wrap_content"                    android:layout_weight="1"                    android:text="3" />            </LinearLayout>            <LinearLayout                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:orientation="horizontal" >                <Button                    android:layout_width="0px"                    android:layout_height="wrap_content"                    android:layout_weight="2"                    android:text="0" />                <Button                    android:layout_width="0px"                    android:layout_height="wrap_content"                    android:layout_weight="1"                    android:text="." />            </LinearLayout>        </LinearLayout>                <LinearLayout                android:layout_width="wrap_content"                android:layout_height="match_parent"                android:layout_weight="1"                android:orientation="horizontal" >            <Button                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:layout_weight="1"                    android:text="=" />                    </LinearLayout>    </LinearLayout></LinearLayout>


原创粉丝点击