首页 > 解决方案 > 表格布局和表格行问题。将文本设置到 Activity Java 类的行中

问题描述

我已尽力解决问题,但我无法得到解决方案。我正在尝试在 android 的表格布局中将文本设置为行。有时它以错误的方向出现,有时它根本不显示。感谢您的帮助和回答。这是代码Java代码。在代码中,我上传了代码的两侧。java部分和xml也是如此。

package com.example.databaseapp;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class SecondActivity extends AppCompatActivity {

   TableLayout tblayoutl;
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        tblayoutl=findViewById(R.id.tblayout);



        String code="123",subject="physics",grades="45",finals="pass";

    TableRow tableRow = new TableRow(this);

    tblayoutl.addView(tableRow);
    TextView textView1 = new TextView(this);
    textView1.setText(code);
    textView1.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
    tableRow.addView(textView1);

    TextView textView2 = new TextView(this);
    textView2.setText(subject);
    textView2.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
    tableRow.addView(textView2);

        TextView textView3 = new TextView(this);
        textView3.setText(grades);
        textView3.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
        tableRow.addView(textView3);


        TextView textView4 = new TextView(this);
        textView4.setText(finals);
        textView4.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
        tableRow.addView(textView4);


/*
         TableRow tableRow = new TableRow(this);
         tblayoutl.addView(tableRow);

        TextView textView2 = new TextView(this);
        TextView textView3 = new TextView(this);
        TextView textView4 = new TextView(this);




        textView1.setGravity(Gravity.LEFT);
        textView1.setTextColor(Color.BLUE);
        textView1.setText(code);
        tableRow.addView(textView1);

        textView2.setGravity(Gravity.LEFT);
        textView2.setTextColor(Color.BLUE);
        textView2.setText(subject);
        tableRow.addView(textView2);



        textView3.setWidth(1);
        textView3.setGravity(Gravity.LEFT);
        textView3.setTextColor(Color.BLUE);
        textView3.setText(grades);
        tableRow.addView(textView3);

        textView4.setWidth(1);
        textView4.setGravity(Gravity.LEFT);
        textView4.setTextColor(Color.BLUE);
        textView4.setText(finals);
        tableRow.addView(textView4);
 */

    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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=".SecondActivity"
    android:splitMotionEvents="true"
    android:orientation="vertical"
    android:background="#FBFBFB"
    >


    <TableLayout
        android:id="@+id/tblayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="#F1E5F3">

        <TableRow>

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="190dp"
                app:srcCompat="@mipmap/a" />
        </TableRow>


        <TableRow android:layout_marginTop="20dp">

            <TextView
                android:id="@+id/welcomeMessage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="This is Online Grade App"
                android:textStyle="bold" />
        </TableRow>


        <TableRow android:layout_marginTop="25dp">

            <TextView
                android:id="@+id/StudentName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text=""
                android:textStyle="bold" />
        </TableRow>

        <TableRow android:layout_marginTop="15dp">

            <TextView
                android:id="@+id/StudentStage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text=""
                android:textStyle="bold" />
        </TableRow>

        <TableRow android:layout_marginTop="30dp">

            <TextView
                android:id="@+id/Code"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Code"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/Subject"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Subject"
                android:textStyle="bold" />


            <TextView
                android:id="@+id/Grades"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Grades"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/finals"
                android:layout_width="match_parent"
                android:layout_height="match_parent"

>                 android:layout_weight="1"

                android:gravity="center"
                android:text="Finals"
                android:textStyle="bold" />
        </TableRow>


    </TableLayout>

</LinearLayout>

在此处输入图像描述

标签: javaandroidxml

解决方案


为每个 TextView设置TableRow.LayoutParams,如下所示。然后宽度会根据initWeight自动调整。

textView1.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1.0f));

推荐阅读