首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >表格布局和表格行问题。从Activity Java Class中将文本设置到行中

表格布局和表格行问题。从Activity Java Class中将文本设置到行中
EN

Stack Overflow用户
提问于 2020-04-18 02:23:07
回答 1查看 122关注 0票数 1

我已经尽了最大的努力来解决这个问题,但是我就是找不到答案。我正在尝试在android的表格布局中将文本设置为行。有时显示方向错误,有时根本不显示。我很感谢你的帮助和回答。以下是Java代码的代码。在代码中,我上传了codes.The的java部分和xml的两个部分。

代码语言:javascript
运行
复制
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>

EN

Stack Overflow用户

发布于 2020-04-18 13:17:58

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

代码语言:javascript
运行
复制
textView1.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61278190

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档