android线性布局之计算器界面设计

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" android:background="#FFFFFF" >
	<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >
    	<EditText  android:id="@+id/msg" android:layout_height="wrap_content" android:layout_width="match_parent"/>
    </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:text="mc" android:layout_weight="1"/>
	    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="m-" android:layout_weight="1"/>
	    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="m+" android:layout_weight="1"/>
	    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="mr" android:layout_weight="1"/>
	</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:text="C" android:layout_weight="1"/>
	    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="+/-" android:layout_weight="1"/>
	    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="/" android:layout_weight="1"/>
	    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="*" android:layout_weight="1"/>
	</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:text="7" android:layout_weight="1"/>
	    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="8" android:layout_weight="1"/>
	    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="9" android:layout_weight="1"/>
	    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="-" android:layout_weight="1"/>
	</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:text="4" android:layout_weight="1"/>
	    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="5" android:layout_weight="1"/>
	    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="6" android:layout_weight="1"/>
	    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="+" android:layout_weight="1"/>
	</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:orientation="vertical" android:layout_weight="3">
			<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:text="1" android:layout_weight="1"/>
		  		  <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2" android:layout_weight="1"/>
		    	  <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="3" android:layout_weight="1"/>
			</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:text="0" android:layout_weight="2"/>
		  		  <Button  android:layout_width="0px" android:layout_height="wrap_content" android:text="." android:layout_weight="1"/>
			</LinearLayout>
	   </LinearLayout>
	   <LinearLayout  android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal" android:layout_weight="1">
	   	 <Button  android:layout_width="match_parent" android:layout_height="match_parent" android:text="=" android:layout_weight="1"/>
	   </LinearLayout>
	</LinearLayout>	
</LinearLayout>

Android开发中,选择合适的计算器界面布局主要取决于你对布局的需求和复杂度。以下是三种常见的布局策略: 1. **线性布局(Linear Layout)**:适合简单的计算器设计,如果每个计算按钮独立排列成一行,线性布局能很好地实现这一需求。你可以将按钮水平排列,并通过设置权重(weight)来控制它们之间的间距。 2. **相对布局(Relative Layout)**:更灵活,可以用于创建多层次的布局,比如将数字键、运算符键和结果区域分开。你可以利用`android:layout_toLeftOf`, `android:layout_toRightOf`等属性来指定控件间的相对位置。 3. **混合使用(混合布局)**:有时候,可能会同时使用线性和相对布局。例如,数字键行可能采用线性布局,而添加运算符的部分则使用相对布局来放置于屏幕底部中心。 4. **网格布局(Grid Layout)**: 如果你需要更多的灵活性和自适应性,特别是对于不同尺寸设备的适配,可以考虑使用GridLayout,它允许你在网格中组织控件。 5. **框架布局(FrameLayout)**: 对于那些需要覆盖其他部分的特殊UI元素(如清除按钮),你可以使用FrameLayout将它们放在最底层,其余的元素作为其子视图显示在其上。 在Java代码中,通过LayoutInflater将XML布局文件解析并应用到Activity或Fragment中。例如: ```java View layout = LayoutInflater.from(this).inflate(R.layout.calculator_layout, null); setContentView(layout); ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值