android使用代码动态做表格
【摘要】 动态生成表格布局
package com.anyikang.emergency120.aek.activity; import android.annotation.SuppressLint;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.support.v7...
动态生成表格布局
-
package com.anyikang.emergency120.aek.activity;
-
-
import android.annotation.SuppressLint;
-
import android.graphics.drawable.Drawable;
-
import android.os.Bundle;
-
import android.support.v7.app.AppCompatActivity;
-
import android.widget.LinearLayout;
-
import android.widget.TableLayout;
-
import android.widget.TableRow;
-
import android.widget.TextView;
-
-
import com.anyikang.emergency120.aek.R;
-
-
public class RescueOrderNotifyActivity extends AppCompatActivity {
-
-
LinearLayout layoutPush;
-
private Drawable drawable;
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
TableLayout tableLayout = new TableLayout(this);
-
drawable = this.getResources().getDrawable(R.drawable.table_border);
-
tableLayout.setDividerDrawable(drawable);
-
tableLayout.setShowDividers(TableLayout.SHOW_DIVIDER_BEGINNING|TableLayout.SHOW_DIVIDER_END|TableLayout.SHOW_DIVIDER_MIDDLE);
-
tableLayout.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));
-
-
for(int i = 0; i < 3; i++)
-
{
-
TableRow tableRow = new TableRow(this);
-
tableRow.setOrientation(LinearLayout.HORIZONTAL);
-
tableRow.setDividerDrawable(drawable);
-
tableRow.setShowDividers(TableLayout.SHOW_DIVIDER_BEGINNING|TableLayout.SHOW_DIVIDER_END|TableLayout.SHOW_DIVIDER_MIDDLE);
-
-
TextView textView = new TextView(this);
-
TextView textView2 = new TextView(this);
-
textView.setText("标题"+i);
-
textView2.setText("标题"+(i+1));
-
textView.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1));
-
textView2.setLayoutParams(new TableRow.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1));
-
-
-
tableRow.addView(textView);
-
tableRow.addView(textView2);
-
tableLayout.addView(tableRow);
-
}
-
setContentView(tableLayout);
-
-
-
}
-
}
在drawable文件夹下创建XML shape资源文件:table_border
-
<?xml version="1.0" encoding="utf-8"?>
-
<shape xmlns:android="http://schemas.android.com/apk/res/android"
-
android:shape="rectangle">
-
<size
-
android:width="1dp"
-
android:height="1dp" />
-
-
<solid android:color="@color/main_bg" />
-
-
</shape>
其中的color自己定义吧,在此不再贴出。
文章来源: blog.csdn.net,作者:冉航--小虾米,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/gaoxiaoweiandy/article/details/105102759
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)