我的课表—android 表格布局应用
- 布局文件
a.完整mainactivity布局文件代码
关键点:在(0,0)的位置,左上角空出一个位置代码
android:layout_column="1"
<TableLayout 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:background="#aabbcc"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:background="#ccbbaa"
android:gravity="center"
android:padding="5dp"
android:text="@string/mycur"
android:textColor="#ff2233"
android:textSize="24sp" />
<TableRow>
<TextView
style="@style/textView"
android:background="#00000000" />
<TextView
style="@style/textView"
android:layout_column="1"
android:text="@string/yi" />
<TextView
style="@style/textView"
android:text="@string/er" />
<TextView
style="@style/textView"
android:text="@string/san" />
<TextView
style="@style/textView"
android:text="@string/si" />
<TextView
style="@style/textView"
android:text="@string/wu" />
<TextView
style="@style/textView"
android:text="@string/liu" />
<TextView
style="@style/textView"
android:text="@string/qi" />
</TableRow>
<TableRow>
<TextView
style="@style/textView"
android:text="@string/first" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
</TableRow>
<TableRow>
<TextView
style="@style/textView"
android:text="@string/second" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
</TableRow>
<TableRow>
<TextView
style="@style/textView"
android:text="@string/third" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
</TableRow>
<TableRow>
<TextView
style="@style/textView"
android:text="@string/forth" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
</TableRow>
<TableRow>
<TextView
style="@style/textView"
android:text="@string/fifth" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
</TableRow>
<TableRow>
<TextView
style="@style/textView"
android:text="@string/sixth" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
<Button style="@style/btn" />
</TableRow>
</TableLayout>
b.自定义布局文件
bg.xml 自定义TextView 边框
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
<style name="textView">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">1dp</item>
<item name="android:textSize">16sp</item>
<item name="android:background">@drawable/bg</item>
</style>
<style name="btn">
<item name="android:layout_width">0dp</item>
<item name="android:layout_weight">1</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">1dp</item>
<item name="android:textSize">16sp</item>
<item name="android:background">@drawable/bg</item>
<item name="android:minHeight">0dp</item>
<item name="android:minWidth">0dp</item>
<item name="android:textColor">#0000ff</item>
<item name="android:onClick">setCourse</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00000000" /><!-- 矩形颜色为透明 -->
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp"/>
<stroke
android:width="2dp"
android:color="#000000" /><!-- 矩形边框为2像素,颜色为黑色 -->
</shape>
2 绑定到指定标签
将onClick属性添加到样式文件 点击空格可以添加课程信息
<item name="android:onClick">setCourse</item>
在Activity中添加一个对应的方法
public void setCourse(View view) {
btn=(Button) view;
Builder courseBuild= new AlertDialog.Builder(this);
courseBuild.setTitle("请输入课程名");
final EditText editText= new EditText(this);
editText.setText(btn.getText().toString());
courseBuild.setView(editText);
courseBuild.setPositiveButton("确定",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
btn.setText(editText.getText().toString());//设置课程
}
});//为对话框添加确定按钮
courseBuild.create().show();
}
3运行效果