Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使Android布局像标记表一样?_Java_Android_Android Layout - Fatal编程技术网

Java 如何使Android布局像标记表一样?

Java 如何使Android布局像标记表一样?,java,android,android-layout,Java,Android,Android Layout,我不知道怎么做,请帮忙 我想在androidxml中创建一个markshit布局视图。 我怎样才能做到这一点,我可以使用ListView使学期变得动态 sem 1 sem 2 sem 3.... Total math 20 25 22 67 science 18 22 24 64 english 18 22 24

我不知道怎么做,请帮忙

我想在androidxml中创建一个markshit布局视图。 我怎样才能做到这一点,我可以使用ListView使学期变得动态

            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

您可以使用
GridLayout
进行此操作或
LinearLayout

如果您使用的是
GridLayout
,则为位置0和位置1,2,3,4,5推送一个空视图。。。列将包含您的学期标题,最后一列索引将是总计

现在,下一行的第一个元素将是您的主题标题,每个for都是相同的

您必须相应地安排模型,以实现适当的数据表示


如果您使用的是
LinearLayout
,那么您的布局将使用
LinearLayout
weightSum和weight属性以xml格式显示。在此布局中,iam使用两种表格布局。一个用于标题,一个用于数据。 数据被包装在scrollview中,因此标题是固定的,数据可以滚动。此外,每一行都有一个隐藏的行,其中存储的是headertext或datatext,它们都很长。通过这样做,我们可以使列均匀填充。否则,我们可能最终导致数据的错误表示

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);
                    }
                }
            }
        }
}
public类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mark_表);
试一试{
DisplayMarkSheet(新的JSONObject(ReturnJsonData());
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
字符串ReturnJsonData(){
返回“{\”SemisterData\”:“+
//在此处指定您的列
“{\'列\”:”+
"[" +
“{\”ColumnName\”:\“\”},+//此列故意为空。第一行第一列将为空
{\'ColumnName\':\'Semister1\'}+
{\'ColumnName\':\'Semister2\'}+
{\'ColumnName\':\'Semister3\'}+
{\'ColumnName\':\'Semister4\'}+
“{\'ColumnName\':\'Semister5\'}],”+
“\“页脚行\”:”+
"[" +
“{\'单元格\”:”+
"[" +
{\'CellValue\':\'Total\'}+
{\'CellValue\':\'160\'}+
“{\'CellValue\':\'200\'”+
{\'CellValue\':\'240\'}+
{\'CellValue\':\'300\'}+
“{\'CellValue\':\'340\'}]}],”+
“\“Rows\”:“+//每个主题一行,行将包含包含数据的单元格
"[" +
“{\'单元格\”:”+
"[" +
{\'CellValue\':\'Mathematics\'}+
{\'CellValue\':\'40\'}+
“{\'CellValue\':\'50\'”+
{\'CellValue\':\'60\'}+
“{\'CellValue\':\'70\'”+
{\'CellValue\':\'80\'}]}+
“{\'单元格\”:”+
"[" +
“{\'CellValue\':\'Science\'”+
{\'CellValue\':\'40\'}+
“{\'CellValue\':\'50\'”+
{\'CellValue\':\'60\'}+
“{\'CellValue\':\'70\'”+
{\'CellValue\':\'80\'}]}+
“{\'单元格\”:”+
"[" +
“{\'CellValue\':\'English\'”+
{\'CellValue\':\'40\'}+
“{\'CellValue\':\'50\'”+
{\'CellValue\':\'60\'}+
“{\'CellValue\':\'70\'”+
{\'CellValue\':\'80\'}]}+
“{\'单元格\”:”+
"[" +
“{\'CellValue\':\'Physics\'”+
{\'CellValue\':\'40\'}+
“{\'CellValue\':\'50\'”+
{\'CellValue\':\'60\'}+
“{\'CellValue\':\'70\'”+
“{\'CellValue\':\'80\'}]}]}}”;
}
公共文本视图GetHeaderTextView(字符串HeaderText){
TextView title=新的TextView(此);
title.setText(HeaderText);
标题:设置重力(重心);
标题.setTextColor(颜色.黑色);
title.setTypeface(Typeface.DEFAULT_BOLD);
标题.背景色(颜色.白色);
title.SettexSize(TypedValue.COMPLEX\u UNIT\u SP,15);
标题.设置填充(10,10,10,10);
返回标题;
}
公共文本视图GetHeaderTextViewHidden(字符串HeaderText){
TextView title=新的TextView(此);
title.setText(HeaderText);
标题:设置重力(重心);
标题.setBackgroundColor(Color.parseColor(#a9a9”);
标题.setTextColor(颜色.黑色);
//title.settext外观(上下文、颜色、行文本);
title.setTypeface(Typeface.DEFAULT_BOLD);
标题.设置高度(0);
title.SettexSize(TypedValue.COMPLEX\u UNIT\u SP,15);
标题.设置填充(10,10,10,10);
返回标题;
}
公共文本视图GetItemTextView(字符串ItemText,字符串ColumnAlign){
TextView text=新的TextView(此);
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);
如果(ColumnAlign.equals),则为else(
![<?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>]