线性布局小实现——计算器的简单布局

来源:互联网 发布:淘宝店铺换身份证 编辑:程序博客网 时间:2024/05/20 21:18

本来是想用相对布局来写计算器的布局,然后莫名其妙的变成了线性布局,心塞。

  • 上代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_height="match_parent"    android:layout_width="match_parent"    android:orientation="vertical">    <RelativeLayout        android:layout_height="10dp"        android:layout_width="match_parent"        android:layout_weight="0.4">        <TextView            android:layout_height="wrap_content"            android:layout_width="wrap_content"            android:text="Calculate布局实现"            android:textColor="#1FC8F8"            android:id="@+id/mainTextView1"            android:textSize="17sp"/>        <TextView            android:layout_height="wrap_content"            android:layout_width="wrap_content"            android:layout_alignParentBottom="true"            android:layout_alignParentRight="true"            android:hint="请输入计算"            android:textSize="40sp"            android:text="计算输出口"/>    </RelativeLayout>    <TextView        android:layout_height="1dp"        android:layout_width="match_parent"        android:background="#000000"/>    <LinearLayout        android:layout_height="40dp"        android:layout_width="match_parent"        android:orientation="vertical"        android:layout_weight="2">        <LinearLayout            android:layout_height="wrap_content"            android:layout_width="match_parent"            android:orientation="horizontal"            android:layout_weight="1.0"            android:gravity="center"            android:layout_marginTop="100dp">            <Button                android:layout_height="70dp"                android:layout_width="5dp"                android:text="7"                android:layout_weight="1.0"/>            <Button                android:layout_height="70dp"                android:layout_width="5dp"                android:text="8"                android:layout_weight="1.0"/>            <Button                android:layout_height="70dp"                android:text="9"                android:layout_weight="1.0"                android:layout_width="5dp"/>            <Button                android:layout_height="70dp"                android:layout_width="5dp"                android:text="÷"                android:layout_weight="1.0"/>            <Button                android:layout_height="70dp"                android:layout_width="5dp"                android:text="→"                android:layout_weight="1.0"/>        </LinearLayout>        <LinearLayout            android:layout_height="wrap_content"            android:layout_width="match_parent"            android:orientation="horizontal"            android:layout_weight="1.0"            android:gravity="center">            <Button                android:layout_height="70dp"                android:layout_width="5dp"                android:text="4"                android:layout_weight="1.0"/>            <Button                android:layout_height="70dp"                android:layout_width="5dp"                android:text="5"                android:layout_weight="1.0"/>            <Button                android:layout_height="70dp"                android:text="6"                android:layout_weight="1.0"                android:layout_width="5dp"/>            <Button                android:layout_height="70dp"                android:layout_width="5dp"                android:text="×"                android:layout_weight="1.0"/>            <Button                android:layout_height="70dp"                android:layout_width="5dp"                android:text="%"                android:layout_weight="1.0"/>        </LinearLayout>        <LinearLayout            android:layout_height="wrap_content"            android:layout_width="match_parent"            android:orientation="horizontal"            android:layout_weight="0.2"            android:gravity="center"            android:layout_marginBottom="90dp">            <LinearLayout                android:layout_height="match_parent"                android:layout_width="wrap_content"                android:orientation="vertical"                android:layout_weight="0.8">                <LinearLayout                    android:layout_height="wrap_content"                    android:layout_width="match_parent"                    android:orientation="horizontal"                    android:layout_weight="1.0"                    android:gravity="center">                    <Button                        android:layout_height="70dp"                        android:layout_width="5dp"                        android:text="1"                        android:layout_weight="1.0"/>                    <Button                        android:layout_height="70dp"                        android:layout_width="5dp"                        android:text="2"                        android:layout_weight="1.0"/>                    <Button                        android:layout_height="70dp"                        android:layout_width="5dp"                        android:text="3"                        android:layout_weight="1.0"/>                    <Button                        android:layout_height="70dp"                        android:text="3"                        android:layout_weight="1.0"                        android:layout_width="5dp"/>                </LinearLayout>                <LinearLayout                    android:layout_height="wrap_content"                    android:layout_width="match_parent"                    android:orientation="horizontal"                    android:layout_weight="1.0">                    <Button                        android:layout_height="70dp"                        android:layout_width="5dp"                        android:text="0"                        android:layout_weight="1.0"/>                    <Button                        android:layout_height="70dp"                        android:layout_width="5dp"                        android:text="00"                        android:layout_weight="1.0"/>                    <Button                        android:layout_height="70dp"                        android:layout_width="5dp"                        android:text="2"                        android:layout_weight="1.0"/>                    <Button                        android:layout_height="70dp"                        android:text="."                        android:layout_weight="1.0"                        android:layout_width="5dp"/>                </LinearLayout>            </LinearLayout>            <Button                android:layout_height="match_parent"                android:layout_width="4dp"                android:text="Button"                android:layout_weight="0.2"/>        </LinearLayout>    </LinearLayout></LinearLayout>
  • 效果图(实际)
    这里写图片描述
  • 效果图(布局界面)
    这里写图片描述
  • 思路
    首先弄一个线性布局(垂直),然后上面一个相对布局下面一个线性布局(垂直)
<LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_height="match_parent"    android:layout_width="match_parent"    android:orientation="vertical">    <RelativeLayout        android:layout_height="10dp"        android:layout_width="match_parent"        android:layout_weight="0.4">    </RelativeLayout>    <LinearLayout        android:layout_height="40dp"        android:layout_width="match_parent"        android:orientation="vertical"        android:layout_weight="2">    </LinearLayout></LinearLayout>

这里就是第一层了。然后再在相对布局里面添加控件(这里设置控件的时候如果不设置绝对布局的宽度的话设置控件属性后布局会蹦,原理水平不够,暂时不知道)之后相对布局下面添加文本,如下

<TextView        android:layout_height="1dp"        android:layout_width="match_parent"        android:background="#000000"/>

设置一条黑线。之后再下面的线性布局里面一直LinearLayout加Button就行了。

  • 总结反思

  1. LinearLayout
    LinearLayout分为垂直和平行两个属性,其实这个理解了就很简单(写这个玩意前我是死都不能理解的就是了)垂直和平行是指的这个布局里面的东西排的方法,而且根据我查得资料好像这两个布局还能影响控件的部分属性(具体学到再说吧)
  2. 小问题
    布局过程对于某些奇怪的问题应该记录下来,比如那个明明已经按比例划分却最后崩了的布局,可能这些事涉及到属性或者布局的本层含义。在考虑布局的时候要全面也要有一定的清晰的思路在弄。
  3. 总结
    布局还是要有选择的,不光是为了渲染的速度,也是为了代码的简介。虽然说上面的代码初学看着很震撼(?)但是我相信选择了不如网格布局之类的一定能够简明很多的。另外布局器的布局好像和实际效果好是有差异个,要安装看看实际效果再下一步。总的一句话,多动手、尝试永远是学语言的最快途径。

PS:aide这个布局文件如果转换成Androlua的布局文件结果好像就不一样了,估计是标题栏的问题来着。

原创粉丝点击