android——相对布局,表格布局

知识为进步之母,而进步又为富强之源泉。这篇文章主要讲述android——相对布局,表格布局相关的知识,希望能为你提供帮助。
1.相对布局RelativeLayout 又称作相对布局,也是一种非常常用的布局。和LinearLayout 的排列规则不同,RelativeLayout 显得更加随意一些,它可以通过相对定位的方式让控件出现在布局的任何位置。也正因为如此,RelativeLayout 中的属性非常多,不过这些属性都是有规律可循的,其实并不难理解和记忆。

< RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > < Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Button 1" /> < Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:text="Button 2" /> < Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Button 3" /> < Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:text="Button 4" /> < Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="Button 5" /> < /RelativeLayout>

效果图为
android——相对布局,表格布局

文章图片

 
 
2.表格布局TableLayout 允许我们使用表格的方式来排列控件,这种布局也不是很常用,你只需要了解一下它的基本用法就可以了。既然是表格,那就一定会有行和列,在设计表格时我们尽量应该让每一行都拥有相同的列数,这样的表格也是最简单的。不过有时候事情并非总会顺从我们的心意,当表格的某行一定要有不相等的列数时,就需要通过合并单元格的方式来应对。

1 < TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 2xmlns:tools="http://schemas.android.com/tools" 3android:layout_width="match_parent" 4android:layout_height="match_parent" 5android:stretchColumns="*" 6tools:context="com.calc.minicalculator.MainActivity" > 7 8 9 10< TextView 11android:id="@+id/textView1" 12android:layout_width="match_parent" 13android:layout_height="109dp" 14android:layout_gravity="center_horizontal|top" 15android:gravity="right|center_vertical" 16android:text="" 17 18android:textSize="40dp" 19android:textStyle="bold" /> 20 21 22< TableRow 23android:layout_weight="1" 24android:id="@+id/tableRow1" 25android:layout_width="wrap_content" 26android:layout_height="match_parent" > 27 28< Button 29android:id="@+id/button1" 30android:layout_width="wrap_content" 31android:layout_height="match_parent" 32android:text="清屏" /> 33 34< Button 35android:id="@+id/button2" 36android:layout_width="wrap_content" 37android:layout_height="match_parent" 38android:text="space" /> 39 40< Button 41android:id="@+id/button3" 42android:layout_width="wrap_content" 43android:layout_height="match_parent" 44android:text="+" /> 45 46< Button 47android:id="@+id/button4" 48android:layout_width="wrap_content" 49android:layout_height="match_parent" 50android:text="-" /> 51 52< /TableRow> 53 54< TableRow 55android:layout_weight="1" 56android:id="@+id/tableRow2" 57android:layout_width="wrap_content" 58android:layout_height="match_parent" > 59 60< Button 61android:id="@+id/button5" 62android:layout_width="wrap_content" 63android:layout_height="match_parent" 64android:text="7" /> 65 66< Button 67android:id="@+id/button6" 68android:layout_width="wrap_content" 69android:layout_height="match_parent" 70android:text="8" /> 71 72< Button 73android:id="@+id/button7" 74android:layout_width="wrap_content" 75android:layout_height="match_parent" 76android:text="9" /> 77 78< Button 79android:id="@+id/button8" 80android:layout_width="wrap_content" 81android:layout_height="match_parent" 82android:text="*" /> 83 84< /TableRow> 85 86< TableRow 87android:layout_weight="1" 88android:id="@+id/tableRow3" 89android:layout_width="wrap_content" 90android:layout_height="match_parent" > 91 92< Button 93android:id="@+id/button9" 94android:layout_width="wrap_content" 95android:layout_height="match_parent" 96android:text="4" /> 97 98< Button 99android:id="@+id/button10" 100android:layout_width="wrap_content" 101android:layout_height="match_parent" 102android:text="5" /> 103 104< Button 105android:id="@+id/button11" 106android:layout_width="wrap_content" 107android:layout_height="match_parent" 108android:text="6" /> 109 110< Button 111android:id="@+id/button12" 112android:layout_width="wrap_content" 113android:layout_height="match_parent" 114android:text="/" /> 115 116< /TableRow> 117 118< TableRow 119android:layout_weight="1" 120android:id="@+id/tableRow4" 121android:layout_width="wrap_content" 122android:layout_height="match_parent" > 123 124< Button 125android:id="@+id/button13" 126android:layout_width="wrap_content" 127android:layout_height="match_parent" 128android:text="1" /> 129 130< Button 131android:id="@+id/button14" 132android:layout_width="wrap_content" 133android:layout_height="match_parent" 134android:text="2" /> 135 136< Button 137android:id="@+id/button15" 138android:layout_width="wrap_content" 139android:layout_height="match_parent" 140android:text="3" /> 141 142< Button 143android:id="@+id/button19" 144android:layout_width="wrap_content" 145android:layout_height="match_parent" 146android:text="." /> 147 148< /TableRow> 149 150< TableRow 151android:layout_weight="1" 152android:id="@+id/tableRow5" 153android:layout_width="wrap_content" 154android:layout_height="match_parent" 155> 156 157< Button 158android:id="@+id/button17" 159android:layout_width="wrap_content" 160android:layout_height="match_parent" 161android:text="0" 162android:layout_span="2" 163/> 164 165< Button 166android:id="@+id/button16" 167android:layout_width="wrap_content" 168android:layout_height="match_parent" 169android:text="=" 170android:layout_span="2"/> 171 172< /TableRow> 173 174 < /TableLayout>

效果图:
android——相对布局,表格布局

文章图片

【android——相对布局,表格布局】 

    推荐阅读