• Android计算器布局


    Android(安桌)计算器布局实现

            ——解决整个屏幕方案

    引言:

        学完了android布局的几种方式,做了一个android计算器。

        我在网上搜索了这方面的资料,发现了布局都有问题,没有充满整个屏幕,只是占了一个部分。

        老师的建议是:设置字体的大小是关键。但是在我设置好字体大小问题,解决屏幕问题后,发现字体居然没有居中。在靠着水平线的左边。而后使用android:gravity的这个属性,没有改变。但是后来发现,使用的TableLayout好像没有这个属性,嵌套使用了一个Layout,才解决了这个问题。

        

    实现效果如下:


    LayOut中的xml文件代码如下:

      1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      2     android:layout_width="fill_parent"
      3     android:layout_height="fill_parent"
      4     android:orientation="vertical" >
      5     
      6     <TextView
      7         android:id="@+id/writerinfo"
      8         android:layout_width="fill_parent"
      9         android:layout_height="wrap_content"
     10         android:layout_marginTop="5dp"
     11         android:textSize="@dimen/textviewsize"
     12         android:gravity="center_horizontal"
     13         android:text="@string/writer" />
     14     
     15     <TextView
     16         android:id="@+id/input"
     17         android:layout_width="fill_parent"
     18         android:layout_height="wrap_content"
     19         android:layout_marginTop="5dp"
     20         android:textSize="@dimen/textviewsize"
     21         android:text="@string/inputhint" />
     22 
     23     <TextView
     24         android:id="@+id/textView2"
     25         android:layout_width="fill_parent"
     26         android:layout_height="wrap_content"
     27         android:layout_marginTop="5dp"
     28         android:textSize="@dimen/textviewsize"
     29         android:text="@string/resulthint" />
     30 
     31     <TableLayout
     32         android:layout_width="fill_parent"
     33         android:layout_height="wrap_content" 
     34         android:stretchColumns="0,1,2,3"
     35         >
     36 
     37         <TableRow
     38             android:id="@+id/tableRow1"
     39             android:layout_width="fill_parent"
     40             android:layout_height="wrap_content" >
     41 
     42             <Button
     43                 android:id="@+id/num7"
     44                 android:layout_width="fill_parent"
     45                 android:layout_height="wrap_content"
     46                 android:textSize="@dimen/buttonsize"
     47                 android:text="7" />
     48 
     49             <Button
     50                 android:id="@+id/num8"
     51                 android:layout_width="fill_parent"
     52                 android:layout_height="wrap_content"
     53                  android:textSize="@dimen/buttonsize"
     54                 android:text="8" />
     55 
     56             <Button
     57                 android:id="@+id/num9"
     58                 android:layout_width="fill_parent"
     59                 android:layout_height="wrap_content"
     60                  android:textSize="@dimen/buttonsize"
     61                 android:text="9" />
     62 
     63             <Button
     64                 android:id="@+id/divide"
     65                 android:layout_width="fill_parent"
     66                 android:layout_height="wrap_content"
     67                 android:textSize="@dimen/buttonsize"
     68                 android:text="/" />
     69             
     70         </TableRow>
     71 
     72         <TableRow
     73             android:id="@+id/tableRow2"
     74             android:layout_width="fill_parent"
     75             android:layout_height="wrap_content" >
     76 
     77             <Button
     78                 android:id="@+id/num4"
     79                 android:layout_width="wrap_content"
     80                 android:layout_height="wrap_content"
     81                  android:textSize="@dimen/buttonsize"
     82                 android:text="4" />
     83 
     84             <Button
     85                 android:id="@+id/num5"
     86                 android:layout_width="wrap_content"
     87                 android:layout_height="wrap_content"
     88                  android:textSize="@dimen/buttonsize"
     89                 android:text="5" />
     90 
     91             <Button
     92                 android:id="@+id/num6"
     93                 android:layout_width="wrap_content"
     94                 android:layout_height="wrap_content"
     95                  android:textSize="@dimen/buttonsize"
     96                 android:text="6" />
     97             
     98             <Button
     99                 android:id="@+id/ride"
    100                 android:layout_width="wrap_content"
    101                 android:layout_height="wrap_content"
    102                  android:textSize="@dimen/buttonsize"
    103                 android:text="*" />
    104 
    105         </TableRow>
    106 
    107         <TableRow
    108             android:id="@+id/tableRow3"
    109             android:layout_width="fill_parent"
    110             android:layout_height="wrap_content" >
    111                 
    112             <Button
    113                 android:id="@+id/num1"
    114                 android:layout_width="wrap_content"
    115                 android:layout_height="wrap_content"
    116                  android:textSize="@dimen/buttonsize"
    117                 android:text="1" />
    118 
    119             <Button
    120                 android:id="@+id/num2"
    121                 android:layout_width="wrap_content"
    122                 android:layout_height="wrap_content"
    123                  android:textSize="@dimen/buttonsize"
    124                 android:text="2" />
    125 
    126             <Button
    127                 android:id="@+id/button11"
    128                 android:layout_width="wrap_content"
    129                 android:layout_height="wrap_content"
    130                  android:textSize="@dimen/buttonsize"
    131                 android:text="3" />
    132 
    133             <Button
    134                 android:id="@+id/sub"
    135                 android:layout_width="wrap_content"
    136                 android:layout_height="wrap_content"
    137                  android:textSize="@dimen/buttonsize"
    138                 android:text="-" />
    139 
    140         </TableRow>
    141 
    142         <TableRow
    143             android:id="@+id/tableRow4"
    144             android:layout_width="fill_parent"
    145             android:layout_height="wrap_content" >
    146 
    147             <Button
    148                 android:id="@+id/num0"
    149                 android:layout_width="wrap_content"
    150                 android:layout_height="wrap_content"
    151                  android:textSize="@dimen/buttonsize"
    152                 android:text="0" />
    153 
    154             <Button
    155                 android:id="@+id/point"
    156                 android:layout_width="wrap_content"
    157                 android:layout_height="wrap_content"
    158                  android:textSize="@dimen/buttonsize"
    159                 android:text="." />
    160 
    161             <Button
    162                 android:id="@+id/add"
    163                 android:layout_width="wrap_content"
    164                 android:layout_height="wrap_content"
    165                  android:textSize="@dimen/buttonsize"
    166                 android:text="+" />
    167 
    168             <Button
    169                 android:id="@+id/equal"
    170                 android:layout_width="wrap_content"
    171                 android:layout_height="wrap_content"
    172                  android:textSize="@dimen/buttonsize"
    173                 android:text="=" />
    174 
    175         </TableRow>
    176     </TableLayout>
    177     <Button
    178                 android:id="@+id/clean"
    179                 android:layout_width="fill_parent"
    180                 android:layout_height="fill_parent"
    181                  android:textSize="@dimen/buttonsize"
    182                 android:text="clean" />
    183     
    184 </LinearLayout>
    View Code
    <dimen name="textviewsize">25sp</dimen>
    <dimen name="buttonsize">45dp</dimen>

    注:如果读者在复制时没有铺满整个屏幕,请更改

      dimens文件中要添加的两个参数。

  • 相关阅读:
    在ASP.NET MVC应用中开发插件框架(中英对照)
    《ASP.NET MVC 5 破境之道》:第一境 ASP.Net MVC5项目初探 — 第一节:运行第一个MVC5项目
    《ASP.NET MVC 5 破境之道》:概述
    量化投资学习笔记17——回归分析:一些问题的处理
    量化投资学习笔记16——回归分析:多元线性回归
    量化投资学习笔记15——回归分析:一元线性回归
    量化投资学习笔记14——机器学习:基础知识
    量化投资学习笔记13——各种指标的绘图、计算及交易策略
    量化投资学习笔记12——时间序列分析实操
    量化投资学习笔记11——关于时间序列你所能做的一切
  • 原文地址:https://www.cnblogs.com/orangebook/p/3516653.html
Copyright © 2020-2023  润新知