如何使Android的布局像marksheets? [英] How to make Android layout like marksheets?
问题描述
我不知道如何使这个,请帮助
我想在Android的XML格式的markshit布局视图。
我怎么能做到,我可以使用ListView控件,使学期动态。
SEM 1 SEM 2 SEM 3 ....总
数学20 25 22 67
科学18 22 24 64
英语18 22 24 64
物理20 25 22 67 总计76 94 92 262
使用两个表格布局
在此布局,IAM。一个标头和一个用于数据。
数据被包裹在滚动视图,所以首部将被固定和数据可以滚动。另外我有一个隐藏的行的每一行,这里是存放这曾经是长HEADERTEXT或datatext。通过这样做,我们可以做出同样的列填充。否则就可能在数据的错误重新presentation结束。
公共类MainActivity扩展AppCompatActivity { @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.mark_sheet); 尝试{
DisplayMarkSheet(新的JSONObject(ReturnJsonData()));
}赶上(JSONException E){
// TODO自动生成catch块
e.printStackTrace();
}
} 串ReturnJsonData(){ 回归{\\SemisterData \\:+
//在这里指定你的专栏
{\\栏目\\:+
[+
{\\的ColumnName \\:\\\\},+ //此列有意空白。第一行第一列将是空白
{\\的ColumnName \\:\\Semister1 \\},+
{\\的ColumnName \\:\\Semister2 \\},+
{\\的ColumnName \\:\\Semister3 \\},+
{\\的ColumnName \\:\\Semister4 \\},+
{\\的ColumnName \\:\\Semister5 \\}],+
\\FooterRows \\:+
[+
{\\细胞\\:+
[+
{\\CellValue \\:\\总计\\},+
{\\CellValue \\:\\160 \\},+
{\\CellValue \\:\\200 \\},+
{\\CellValue \\:\\240 \\},+
{\\CellValue \\:\\300 \\},+
{\\CellValue \\:\\340 \\}]}],+
\\行\\:每个科目+ //一行,行会与数据的单元格
[+
{\\细胞\\:+
[+
{\\CellValue \\:\\数学\\},+
{\\CellValue \\:\\40 \\},+
{\\CellValue \\:\\50 \\},+
{\\CellValue \\:\\60 \\},+
{\\CellValue \\:\\70 \\},+
{\\CellValue \\:\\80 \\}]},+ {\\细胞\\:+
[+
{\\CellValue \\:\\科学\\},+
{\\CellValue \\:\\40 \\},+
{\\CellValue \\:\\50 \\},+
{\\CellValue \\:\\60 \\},+
{\\CellValue \\:\\70 \\},+
{\\CellValue \\:\\80 \\}]},+
{\\细胞\\:+
[+
{\\CellValue \\:\\英语\\},+
{\\CellValue \\:\\40 \\},+
{\\CellValue \\:\\50 \\},+
{\\CellValue \\:\\60 \\},+
{\\CellValue \\:\\70 \\},+
{\\CellValue \\:\\80 \\}]},+
{\\细胞\\:+
[+
{\\CellValue \\:\\物理学\\},+
{\\CellValue \\:\\40 \\},+
{\\CellValue \\:\\50 \\},+
{\\CellValue \\:\\60 \\},+
{\\CellValue \\:\\70 \\},+
{\\CellValue \\:\\80 \\}]}]}};
} 公众的TextView GetHeaderTextView(字符串的HeaderText){
TextView的标题=新的TextView(本);
title.setText(的HeaderText);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.BLACK);
title.setTypeface(Typeface.DEFAULT_BOLD);
title.setBackgroundColor(Color.WHITE);
title.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
title.setPadding(10,10,10,10);
返回称号;
} 公众的TextView GetHeaderTextViewHidden(字符串的HeaderText){
TextView的标题=新的TextView(本);
title.setText(的HeaderText);
title.setGravity(Gravity.CENTER);
title.setBackgroundColor(Color.parseColor(#A9A9A9));
title.setTextColor(Color.BLACK);
// title.setTextAppearance(背景下,color.RowText);
title.setTypeface(Typeface.DEFAULT_BOLD);
title.setHeight(0);
title.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
title.setPadding(10,10,10,10);
返回称号;
} 公众的TextView GetItemTextView(字符串ItemText,字符串ColumnAlign){
TextView的文本=新的TextView(本);
text.setText(ItemText); 如果(ColumnAlign.equals(L))
text.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
否则如果(ColumnAlign.equals(R))
text.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
否则如果(ColumnAlign.equals(C))
text.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text.setPadding(10,10,10,10);
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
// text.setCompoundDrawablesWithIntrinsicBounds(0,0,
// R.drawable.next_image,0);
text.setTextColor(Color.BLACK); 返回文本;
} 公共静态的TextView GetItemTextViewHidden(上下文的背景下,字符串ItemText,字符串ColumnAlign){
TextView的文本=新的TextView(背景);
text.setText(ItemText);
如果(ColumnAlign.equals(L))
text.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
否则如果(ColumnAlign.equals(R))
text.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
否则如果(ColumnAlign.equals(C))
text.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text.setPadding(10,10,10,10);
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setHeight(0);
text.setTextSize(TypedValue.COMPLEX_UNIT_SP,15);
text.setTextColor(Color.WHITE);
返回文本;
} 公共无效DisplayMarkSheet(JSONObject的JSON)抛出JSONException {
TableLayout headerTable =(TableLayout)findViewById(R.id.header);
TableLayout的dataTable =(TableLayout)findViewById(R.id.maintable); headerTable.setStretchAllColumns(真);
dataTable.setStretchAllColumns(真); 的TableRow headerRow =新的TableRow(本);
的TableRow invisibleHeaderRow =新的TableRow(本); JSONArray columnsArray = json.getJSONObject(SemisterData)getJSONArray(列)。
JSONArray rowsArray = json.getJSONObject(SemisterData)getJSONArray(行)。
JSONArray footerArray = json.getJSONObject(SemisterData)getJSONArray(FooterRows); //创建标题行和添加列
如果(json.getJSONObject(SemisterData)。ISNULL(列)== FALSE){
headerRow.setBackgroundColor(Color.WHITE);
的for(int i = 0; I< = columnsArray.length() - 1;我++){
headerRow.addView(GetHeaderTextView(columnsArray.getJSONObject㈠.getString(的ColumnName)));
invisibleHeaderRow.addView(GetHeaderTextViewHidden(columnsArray.getJSONObject(i).getString(\"ColumnName\")));
}
headerTable.addView(headerRow);
headerTable.addView(invisibleHeaderRow);
} //创建数据行并添加数据
串HEADERTEXT =;
串rowText;
如果(json.getJSONObject(SemisterData)。ISNULL(行)== FALSE){
的for(int i = 0; I< = rowsArray.length() - 1;我++){
数据行的TableRow =新的TableRow(本);
的TableRow invisibledataRow =新的TableRow(本); dataRow.setBackgroundColor(Color.WHITE); INT cellsLength = 0;
如果(rowsArray.getJSONObject(I).isNull(细胞)== FALSE) {
cellsLength = rowsArray.getJSONObject(ⅰ).getJSONArray(单元)的长度();
对于(INT K = 0; K< = cellsLength - 1; k ++){ dataRow.addView(GetItemTextView(rowsArray.getJSONObject(i).getJSONArray(\"Cells\").getJSONObject(k).getString(\"CellValue\"), C));
rowText = rowsArray.getJSONObject(I).getJSONArray(细胞)getJSONObject(K).getString(CellValue); HEADERTEXT =(字符串)((的TextView)invisibleHeaderRow.getChildAt(K))的getText()。
如果(headerText.length()> rowText.length())
invisibledataRow.addView(GetItemTextViewHidden(这一点,HEADERTEXT,C));
其他{
invisibledataRow.addView(GetItemTextViewHidden(在此,rowsArray.getJSONObject(ⅰ).getJSONArray(细胞)getJSONObject(k)的.getString(CellValue),C)。);
((的TextView)invisibleHeaderRow.getChildAt(K))的setText(rowText)。
} }
dataRow.setPadding(0,5,0,5);
dataTable.addView(数据行);
dataTable.addView(invisibledataRow);
}
} } //创建尾行并填充数据
如果(json.getJSONObject(SemisterData)。ISNULL(FooterRows)== FALSE){
的for(int i = 0; I< = footerArray.length() - 1;我++){
的TableRow footerRow =新的TableRow(本);
footerRow.setBackgroundColor(Color.WHITE);
如果(footerArray.getJSONObject(I).isNull(细胞)== FALSE){
对于(INT K = 0; K< = footerArray.getJSONObject(I).getJSONArray(细胞),长度() - 1,说明k ++){
footerRow.addView(GetItemTextView(footerArray.getJSONObject(i).getJSONArray(\"Cells\").getJSONObject(k).getString(\"CellValue\"), C));
footerRow.setPadding(0,5,0,5); 如果(headerText.length()< footerArray.getJSONObject(I).getJSONArray(细胞)getJSONObject(K).getString(CellValue),长度())
((TextView中) invisibleHeaderRow.getChildAt(k)).setText(footerArray.getJSONObject(i).getJSONArray(\"Cells\").getJSONObject(k).getString(\"CellValue\"));
}
dataTable.addView(footerRow);
}
}
}
}
}
XML文件
[<!?XML版本=1.0编码=UTF-8&GT?;
< HorizontalScrollView的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / WidgetsHSV
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:fillViewport =真正的> <的LinearLayout
机器人:ID =@ + ID / GridLL
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直> < TableLayout
机器人:ID =@ + ID /头
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@机器人:彩色/黑白>
< / TableLayout> <滚动型
机器人:ID =@ + ID / table_scroll
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:fillViewport =真正的> < TableLayout
机器人:ID =@ + ID / maintable
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@机器人:彩色/黑白>
< / TableLayout>
< /滚动型>
< / LinearLayout中>< / HorizontalScrollView>]
输出
i have no idea how to make this, please help
I want to make a markshit layout view in android xml. How could i make it, can i use ListView to make semester dynamic..
sem 1 sem 2 sem 3.... Total
math 20 25 22 67
science 18 22 24 64
english 18 22 24 64
physical 20 25 22 67
Total 76 94 92 262
In this layout, iam using two table layouts. One for header and one for data. data is wrapped in scrollview, so the header will be fixed and data can be scrolled. Also i have a hidden row for every row, where is store the headertext or datatext which ever is long. By doing this, we can make columns populate equally. otherwise we may end up in wrong representation of data.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mark_sheet);
try {
DisplayMarkSheet(new JSONObject(ReturnJsonData()));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String ReturnJsonData() {
return "{\"SemisterData\":" +
//Specify your columns here
"{\"Columns\":" +
"[" +
"{\"ColumnName\":\"\"}," + //This column is intentionally blank. First Row, First Column will be blank
"{\"ColumnName\":\"Semister1\"}," +
"{\"ColumnName\":\"Semister2\"}," +
"{\"ColumnName\":\"Semister3\"}," +
"{\"ColumnName\":\"Semister4\"}," +
"{\"ColumnName\":\"Semister5\"}]," +
"\"FooterRows\":" +
"[" +
"{\"Cells\":" +
"[" +
"{\"CellValue\":\"Total\"}," +
"{\"CellValue\":\"160\"}," +
"{\"CellValue\":\"200\"}," +
"{\"CellValue\":\"240\"}," +
"{\"CellValue\":\"300\"}," +
"{\"CellValue\":\"340\"}]}]," +
"\"Rows\":" + //One row for each subject, Rows will have cells with data
"[" +
"{\"Cells\":" +
"[" +
"{\"CellValue\":\"Maths\"}," +
"{\"CellValue\":\"40\"}," +
"{\"CellValue\":\"50\"}," +
"{\"CellValue\":\"60\"}," +
"{\"CellValue\":\"70\"}," +
"{\"CellValue\":\"80\"}]}," +
"{\"Cells\":" +
"[" +
"{\"CellValue\":\"Science\"}," +
"{\"CellValue\":\"40\"}," +
"{\"CellValue\":\"50\"}," +
"{\"CellValue\":\"60\"}," +
"{\"CellValue\":\"70\"}," +
"{\"CellValue\":\"80\"}]}," +
"{\"Cells\":" +
"[" +
"{\"CellValue\":\"English\"}," +
"{\"CellValue\":\"40\"}," +
"{\"CellValue\":\"50\"}," +
"{\"CellValue\":\"60\"}," +
"{\"CellValue\":\"70\"}," +
"{\"CellValue\":\"80\"}]}," +
"{\"Cells\":" +
"[" +
"{\"CellValue\":\"Physics\"}," +
"{\"CellValue\":\"40\"}," +
"{\"CellValue\":\"50\"}," +
"{\"CellValue\":\"60\"}," +
"{\"CellValue\":\"70\"}," +
"{\"CellValue\":\"80\"}]}]}}";
}
public TextView GetHeaderTextView(String HeaderText) {
TextView title = new TextView(this);
title.setText(HeaderText);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.BLACK);
title.setTypeface(Typeface.DEFAULT_BOLD);
title.setBackgroundColor(Color.WHITE);
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
title.setPadding(10, 10, 10, 10);
return title;
}
public TextView GetHeaderTextViewHidden(String HeaderText) {
TextView title = new TextView(this);
title.setText(HeaderText);
title.setGravity(Gravity.CENTER);
title.setBackgroundColor(Color.parseColor("#A9A9A9"));
title.setTextColor(Color.BLACK);
// title.setTextAppearance(context, color.RowText);
title.setTypeface(Typeface.DEFAULT_BOLD);
title.setHeight(0);
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
title.setPadding(10, 10, 10, 10);
return title;
}
public TextView GetItemTextView(String ItemText, String ColumnAlign) {
TextView text = new TextView(this);
text.setText(ItemText);
if (ColumnAlign.equals("L"))
text.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
else if (ColumnAlign.equals("R"))
text.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
else if (ColumnAlign.equals("C"))
text.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text.setPadding(10, 10, 10, 10);
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
// text.setCompoundDrawablesWithIntrinsicBounds(0, 0,
// R.drawable.next_image, 0);
text.setTextColor(Color.BLACK);
return text;
}
public static TextView GetItemTextViewHidden(Context context, String ItemText, String ColumnAlign) {
TextView text = new TextView(context);
text.setText(ItemText);
if (ColumnAlign.equals("L"))
text.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
else if (ColumnAlign.equals("R"))
text.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
else if (ColumnAlign.equals("C"))
text.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
text.setPadding(10, 10, 10, 10);
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setHeight(0);
text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
text.setTextColor(Color.WHITE);
return text;
}
public void DisplayMarkSheet(JSONObject json) throws JSONException {
TableLayout headerTable = (TableLayout) findViewById(R.id.header);
TableLayout dataTable = (TableLayout) findViewById(R.id.maintable);
headerTable.setStretchAllColumns(true);
dataTable.setStretchAllColumns(true);
TableRow headerRow = new TableRow(this);
TableRow invisibleHeaderRow = new TableRow(this);
JSONArray columnsArray = json.getJSONObject("SemisterData").getJSONArray("Columns");
JSONArray rowsArray = json.getJSONObject("SemisterData").getJSONArray("Rows");
JSONArray footerArray = json.getJSONObject("SemisterData").getJSONArray("FooterRows");
// Create header row and add Columns
if (json.getJSONObject("SemisterData").isNull("Columns") == false) {
headerRow.setBackgroundColor(Color.WHITE);
for (int i = 0; i <= columnsArray.length() - 1; i++) {
headerRow.addView(GetHeaderTextView(columnsArray.getJSONObject(i).getString("ColumnName")));
invisibleHeaderRow.addView(GetHeaderTextViewHidden(columnsArray.getJSONObject(i).getString("ColumnName")));
}
headerTable.addView(headerRow);
headerTable.addView(invisibleHeaderRow);
}
// Create data row and add data
String headerText = "";
String rowText;
if (json.getJSONObject("SemisterData").isNull("Rows") == false) {
for (int i = 0; i <= rowsArray.length() - 1; i++) {
TableRow dataRow = new TableRow(this);
TableRow invisibledataRow = new TableRow(this);
dataRow.setBackgroundColor(Color.WHITE);
int cellsLength = 0;
if (rowsArray.getJSONObject(i).isNull("Cells") == false)
{
cellsLength = rowsArray.getJSONObject(i).getJSONArray("Cells").length();
for (int k = 0; k <= cellsLength - 1; k++) {
dataRow.addView(GetItemTextView(rowsArray.getJSONObject(i).getJSONArray("Cells").getJSONObject(k).getString("CellValue"), "C"));
rowText = rowsArray.getJSONObject(i).getJSONArray("Cells").getJSONObject(k).getString("CellValue");
headerText = (String) ((TextView) invisibleHeaderRow.getChildAt(k)).getText();
if (headerText.length() > rowText.length())
invisibledataRow.addView(GetItemTextViewHidden(this, headerText, "C"));
else {
invisibledataRow.addView(GetItemTextViewHidden(this, rowsArray.getJSONObject(i).getJSONArray("Cells").getJSONObject(k).getString("CellValue"), "C"));
((TextView) invisibleHeaderRow.getChildAt(k)).setText(rowText);
}
}
dataRow.setPadding(0, 5, 0, 5);
dataTable.addView(dataRow);
dataTable.addView(invisibledataRow);
}
}
}
// Create footer row and populate data
if (json.getJSONObject("SemisterData").isNull("FooterRows") == false) {
for (int i = 0; i <= footerArray.length() - 1; i++) {
TableRow footerRow = new TableRow(this);
footerRow.setBackgroundColor(Color.WHITE);
if (footerArray.getJSONObject(i).isNull("Cells") == false) {
for (int k = 0; k <= footerArray.getJSONObject(i).getJSONArray("Cells").length() - 1; k++) {
footerRow.addView(GetItemTextView(footerArray.getJSONObject(i).getJSONArray("Cells").getJSONObject(k).getString("CellValue"), "C"));
footerRow.setPadding(0, 5, 0, 5);
if (headerText.length() < footerArray.getJSONObject(i).getJSONArray("Cells").getJSONObject(k).getString("CellValue").length())
((TextView) invisibleHeaderRow.getChildAt(k)).setText(footerArray.getJSONObject(i).getJSONArray("Cells").getJSONObject(k).getString("CellValue"));
}
dataTable.addView(footerRow);
}
}
}
}
}
XML File
![<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/WidgetsHSV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:id="@+id/GridLL"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black" >
</TableLayout>
<ScrollView
android:id="@+id/table_scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<TableLayout
android:id="@+id/maintable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black" >
</TableLayout>
</ScrollView>
</LinearLayout>
</HorizontalScrollView>]
Output
这篇关于如何使Android的布局像marksheets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!