首页 > 解决方案 > 如何将动态表格布局设置为特定布局的全屏?

问题描述

我正在尝试使用 php 获取数据库,我获取并显示在动态表布局中,根据数据库中的行数创建的行,但表布局位于屏幕中央,但我需要它适合在手机/标签的整个屏幕中。

我试图更改特定屏幕中使用的所有布局的高度和宽度,但没有任何改变,谁能帮我解决这个问题。

这是我的 xml 文件。

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Colleagues_Schedule">


    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="8">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical">
            <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#3d455b"
                android:layout_alignParentLeft="true" >

                <HorizontalScrollView
                    android:id="@+id/hscrll1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" >

                    <RelativeLayout
                        android:id="@+id/RelativeLayout1"
                        android:layout_width="fill_parent"
                        android:layout_gravity="center"
                        android:layout_height="fill_parent"
                        android:orientation="vertical" >

                        <TableLayout
                            android:id="@+id/table_main"
                            android:layout_margin="1dp"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_centerHorizontal="true" >
                        </TableLayout>
                    </RelativeLayout>
                </HorizontalScrollView>
            </ScrollView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center_horizontal"
                android:orientation="horizontal">


                <Button
                    android:onClick="Back"
                    android:id="@+id/back"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="PREVIOUS" />

                <Button
                    android:onClick="Home"
                    android:id="@+id/home"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="HOME" />
                <Button android:onClick="Next"
                    android:id="@+id/next"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="NEXT" />
            </LinearLayout>


            </LinearLayout>

    </LinearLayout>


</android.support.constraint.ConstraintLayout>

主要活动

    package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.nex3z.notificationbadge.NotificationBadge;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import static java.util.Calendar.DATE;

public class Colleagues_Schedule extends AppCompatActivity {
    int i;
    String result;
    int size=20;
    Context context;
    int count = 0;
    NotificationBadge mBadge;
    Button buttonname, btnlogout, btntoday, btnyesterday, btntomorrow;
    JSONArray jsonArray_New = null;
    String[] ScheduleDates, StartTimes, Endtimes,Names;
    String Send_Yesterday, Send_Today, Send_Tomorrow;
    String ScheduleDate, StartTime, Endtime, Today, Yesterday, Tomarrow, Date,Name;
    java.util.Date date1, Todaydate, Yesterdaydate, Tomarrowdate,date2;
    TextView yes_date, yes_ST, yes_ET, tod_date, tod_ST, tod_ET, tom_date, tom_ST, tom_ET;
    private ArrayList<String> ScheduleDateArray = new ArrayList<>();
    private ArrayList<String> StartTimeArray = new ArrayList<>();
    private ArrayList<String> EndtimeArray = new ArrayList<>();
    private ArrayList<String> NameArray = new ArrayList<>();
    SharedPreferences mPrefs;

    GridLayout mainGrid2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_colleagues__schedule);


        result = getIntent().getStringExtra("rootJson");

        init();

    }

    @SuppressLint("ResourceType")
    public void init() {
        TableLayout stk = (TableLayout) findViewById(R.id.table_main);
        TableRow tbrow0 = new TableRow(this);

        TextView tv0 = new TextView(this);
        tv0.setText(" Name ");//date
        tv0.setTextColor(Color.WHITE);
        tbrow0.addView(tv0);
        TextView tv1 = new TextView(this);
        tv1.setText(" Date ");//name
        tv1.setTextColor(Color.WHITE);
        tbrow0.addView(tv1);
        TextView tv2 = new TextView(this);
        tv2.setText(" Start Time ");//starttime
        tv2.setTextColor(Color.WHITE);
        tbrow0.addView(tv2);
        TextView tv3 = new TextView(this);
        tv3.setText(" End Time ");//endtime
        tv3.setTextColor(Color.WHITE);
        tbrow0.addView(tv3);
        stk.addView(tbrow0);



        try {
            JSONObject jsonObjects = new JSONObject(result);
            jsonArray_New = jsonObjects.getJSONArray("VIEW_SCHEDULE");
            //Fetching the data from the php file
            try {
                for (int i = 0; i < jsonArray_New.length(); i++) {
                    JSONObject jsonObject = jsonArray_New.getJSONObject(i);
                    Name = jsonObject.getString("Name");
                    NameArray.add(Name);
                    ScheduleDate = jsonObject.getString("ScheduleDate");
                    ScheduleDateArray.add(ScheduleDate);
                    StartTime = jsonObject.getString("StartTime");
                    StartTimeArray.add(StartTime);
                    Endtime = jsonObject.getString("Endtime");
                    EndtimeArray.add(Endtime);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //Initilize the arraylist to array
            Names = new String[NameArray.size()];
            ScheduleDates = new String[ScheduleDateArray.size()];
            StartTimes = new String[StartTimeArray.size()];
            Endtimes = new String[EndtimeArray.size()];

            for (int i = 0; i < ScheduleDateArray.size(); i++) {
                Names[i] = NameArray.get(i);
                ScheduleDates[i] = ScheduleDateArray.get(i);
                StartTimes[i] = StartTimeArray.get(i);
                Endtimes[i] = EndtimeArray.get(i);
            }

            //Search for Today schedule
            for(int i=0;i<jsonArray_New.length();i++)
            {
                TableRow tbrow = new TableRow(this);
                tbrow.setBackgroundResource(R.drawable.row_border);
                TextView t1v = new TextView(this);
                TextView t2v = new TextView(this);
                TextView t3v = new TextView(this);
                TextView t4v = new TextView(this);

                tbrow.setBackgroundResource(R.drawable.row_border);

                t1v.setText(Names[i]);
                t1v.setTextColor(Color.WHITE);
                t1v.setGravity(Gravity.CENTER);
                tbrow.addView(t1v);


                t2v.setText(ScheduleDates[i] );
                t2v.setTextColor(Color.WHITE);
                t2v.setGravity(Gravity.CENTER);
                tbrow.addView(t2v);

                t3v.setText( StartTimes[i]);
                t3v.setTextColor(Color.WHITE);
                t3v.setGravity(Gravity.CENTER);
                tbrow.addView(t3v);

                t4v.setText(Endtimes[i]);
                t4v.setTextColor(Color.WHITE);
                t4v.setGravity(Gravity.CENTER);
                tbrow.addView(t4v);
                stk.addView(tbrow);

                tbrow.setClickable(true);  //allows you to select a specific row

                tbrow.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                        TableRow tablerow = (TableRow) view;
                        TextView sample = (TextView) tablerow.getChildAt(1);
                        String result=sample.getText().toString();

                        Toast toast = Toast.makeText(Colleagues_Schedule.this, result, Toast.LENGTH_LONG);
                        toast.show();
                    }
                });

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
}

}

这是我的输出屏幕。

输出画面

标签: androidandroid-layout

解决方案


尝试为您动态添加的每一行提供布局参数并更新您的init()函数,如下所示:

@SuppressLint("ResourceType")
public void init() {
    TableLayout stk = (TableLayout) findViewById(R.id.table_main);
    TableRow tbrow0 = new TableRow(this);
    //added layout params and weight
    tbrow0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT, 1.0f));

    TextView tv0 = new TextView(this);
    tv0.setText(" Name ");//date
    tv0.setTextColor(Color.WHITE);
    //added layout params and weight
    tv0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
    tbrow0.addView(tv0);

    TextView tv1 = new TextView(this);
    tv1.setText(" Date ");//name
    tv1.setTextColor(Color.WHITE);
    //added layout params and weight
    tv1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
    tbrow0.addView(tv1);

    TextView tv2 = new TextView(this);
    tv2.setText(" Start Time ");//starttime
    tv2.setTextColor(Color.WHITE);
    //added layout params and weight
    tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
    tbrow0.addView(tv2);

    TextView tv3 = new TextView(this);
    tv3.setText(" End Time ");//endtime
    tv3.setTextColor(Color.WHITE);
    //added layout params and weight
    tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
    tbrow0.addView(tv3);

    //added layout params and weight
    stk.addView(tbrow0, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

    try {
        JSONObject jsonObjects = new JSONObject(result);
        jsonArray_New = jsonObjects.getJSONArray("VIEW_SCHEDULE");
        //Fetching the data from the php file
        try {
            for (int i = 0; i < jsonArray_New.length(); i++) {
                JSONObject jsonObject = jsonArray_New.getJSONObject(i);
                Name = jsonObject.getString("Name");
                NameArray.add(Name);
                ScheduleDate = jsonObject.getString("ScheduleDate");
                ScheduleDateArray.add(ScheduleDate);
                StartTime = jsonObject.getString("StartTime");
                StartTimeArray.add(StartTime);
                Endtime = jsonObject.getString("Endtime");
                EndtimeArray.add(Endtime);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //Initilize the arraylist to array
        Names = new String[NameArray.size()];
        ScheduleDates = new String[ScheduleDateArray.size()];
        StartTimes = new String[StartTimeArray.size()];
        Endtimes = new String[EndtimeArray.size()];

        for (int i = 0; i < ScheduleDateArray.size(); i++) {
            Names[i] = NameArray.get(i);
            ScheduleDates[i] = ScheduleDateArray.get(i);
            StartTimes[i] = StartTimeArray.get(i);
            Endtimes[i] = EndtimeArray.get(i);
        }

        //Search for Today schedule
        for (int i = 0; i < jsonArray_New.length(); i++) {
            TableRow tbrow = new TableRow(this);
            tbrow.setBackgroundResource(R.drawable.row_border);
    //added layout params and weight
            tbrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
            TextView t1v = new TextView(this);
            TextView t2v = new TextView(this);
            TextView t3v = new TextView(this);
            TextView t4v = new TextView(this);

            tbrow.setBackgroundResource(R.drawable.row_border);

            t1v.setText(Names[i]);
            t1v.setTextColor(Color.WHITE);
            t1v.setGravity(Gravity.CENTER);
    //added layout params and weight
            t1v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
            tbrow.addView(t1v);

            t2v.setText(ScheduleDates[i]);
            t2v.setTextColor(Color.WHITE);
            t2v.setGravity(Gravity.CENTER);
    //added layout params and weight
            t2v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
            tbrow.addView(t2v);

            t3v.setText(StartTimes[i]);
            t3v.setTextColor(Color.WHITE);
            t3v.setGravity(Gravity.CENTER);
    //added layout params and weight
            t3v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
            tbrow.addView(t3v);

            t4v.setText(Endtimes[i]);
            t4v.setTextColor(Color.WHITE);
            t4v.setGravity(Gravity.CENTER);
    //added layout params and weight
            t4v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
            tbrow.addView(t4v);

    //added layout params and weight
            stk.addView(tbrow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

            tbrow.setClickable(true);  //allows you to select a specific row

            tbrow.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    TableRow tablerow = (TableRow) view;
                    TextView sample = (TextView) tablerow.getChildAt(1);
                    String result = sample.getText().toString();

                    Toast toast = Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG);
                    toast.show();
                }
            });

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

并提供android:fillViewport="true"给您id=scrollView1ScrollViewid= hscrll1HorizontalScrollView


推荐阅读