使用TableLayout(表格布局管理器)、TableRow(表格行布局管理器)创建一个简单的Android应用。一个评分组件。
1.打开Android studio创建一个Android应用,Application name取名为TableLayout,Company Domain取名为Example.com
2.Minimum SDK选择API 18:Android 4.3
3.选择Empty Activity
4.不需要修改Activity Name,使用默认值,单击“finish”按钮。
修改res.layout目录下的布局文件“activity_main.xml”
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_column="1"
android:text="Time" />
<TextClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textClock"
android:layout_column="2"/>
</TableRow>
<TableRow>
<TextView
android:text="First Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"/>
<EditText
android:width="200px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"/>
</TableRow>
<TableRow>
<TextView
android:text="Last Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"/>
<EditText
android:width="100px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--评分组件-->
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--确定按钮-->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/button"
android:layout_column="2"/>
</TableRow>
</TableLayout>
使用Nexus S API 30模拟器。
运行截图如下:
点击可以滑动星星数量。
由于没有设置时间,时间是错误的。
七种布局管理器:
在一个Android应用程序中,用户界面通过View和ViewGroup对象构建。Android中有很多种View和ViewGroup,它们都继承自View类。View对象是Android平台上表示用户界面的基本单元。布局方式是指一组View元素如何布局,准确地说是一个ViewGroup中包含的一些View如何布局。这里介绍的View的布局方式的类都直接或间接继承自ViewGroup类,如图。
其实,所有的布局方式都可以归类为ViewGroup的5个类别,即ViewGroup的5个直接子类。其他的一些布局都扩展自这5个类。