表格布局和表格行问题。从Activity Java类将文本设置到行中
我已经尽力解决这个问题,但我没有办法解决。我正在尝试在android的表格布局中将文本设置为行。有时它以错误的方向出现,有时它根本不显示。我感谢你的帮助和回答。下面是代码Java代码。在代码中,我上传了代码的两个方面:java部分和xml表格布局和表格行问题。从Activity Java类将文本设置到行中,java,android,xml,Java,Android,Xml,我已经尽力解决这个问题,但我没有办法解决。我正在尝试在android的表格布局中将文本设置为行。有时它以错误的方向出现,有时它根本不显示。我感谢你的帮助和回答。下面是代码Java代码。在代码中,我上传了代码的两个方面:java部分和xml package com.example.databaseapp; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Color; import android.
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>
package com.example.databaseapp;
导入androidx.appcompat.app.appcompat活动;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.view.Gravity;
导入android.widget.TableLayout;
导入android.widget.TableRow;
导入android.widget.TextView;
公共类SecondActivity扩展了AppCompatActivity{
表布局tblayoutl;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_秒);
tblayoutl=findViewById(R.id.tblayout);
字符串code=“123”、subject=“physics”、grades=“45”、final=“pass”;
TableRow TableRow=新的TableRow(本);
tblayoutl.addView(tableRow);
TextView textView1=新的TextView(此);
textView1.setText(代码);
textView1.setLayoutParams(新的TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_内容,TableLayout.LayoutParams.WRAP_内容));
tableRow.addView(textView1);
TextView textView2=新的TextView(此);
textView2.setText(主题);
textView2.setLayoutParams(新的TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_内容,TableLayout.LayoutParams.WRAP_内容));
tableRow.addView(textView2);
TextView textView3=新的TextView(此);
textView3.setText(等级);
textView3.setLayoutParams(新的TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_内容,TableLayout.LayoutParams.WRAP_内容));
tableRow.addView(textView3);
TextView textView4=新的TextView(此);
textView4.setText(决赛);
textView4.setLayoutParams(新的TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_内容,TableLayout.LayoutParams.WRAP_内容));
tableRow.addView(textView4);
/*
TableRow TableRow=新的TableRow(本);
tblayoutl.addView(tableRow);
TextView textView2=新的TextView(此);
TextView textView3=新的TextView(此);
TextView textView4=新的TextView(此);
textView1.setGravity(Gravity.LEFT);
textView1.setTextColor(Color.BLUE);
textView1.setText(代码);
tableRow.addView(textView1);
textView2.setGravity(Gravity.LEFT);
textView2.setTextColor(Color.BLUE);
textView2.setText(主题);
tableRow.addView(textView2);
textView3.setWidth(1);
textView3.setGravity(Gravity.LEFT);
textView3.setTextColor(Color.BLUE);
textView3.setText(等级);
tableRow.addView(textView3);
textView4.setWidth(1);
textView4.setGravity(Gravity.LEFT);
textView4.setTextColor(Color.BLUE);
textView4.setText(决赛);
tableRow.addView(textView4);
*/
}
}
android:layout_weight=“1”
android:gravity=“center”
android:text=“决赛”
android:textStyle=“bold”/>
为每个文本视图设置TableRow.LayoutParams,如下所示。然后,宽度将根据初始重量自动调整
textView1.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
为每个文本视图设置TableRow.LayoutParams,如下所示。然后,宽度将根据初始重量自动调整
textView1.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 1.0f));
非常感谢你的重播,问题解决了。另外,你知道用简单的方法在桌子周围创建边框的技巧吗。我已经读到我必须在可绘制的地方创建xml,但这有点奇怪,你知道最简单的方法吗。再次感谢您的帮助。@MiranBCM谢谢您。但我担心,在stackoverflow中不欢迎在后面附加另一个问题。如果你想解决这个问题,请。。。尽管通过将表格放在框架布局中(带有背景和填充),它还是可以实现的。谢谢我解决了问题。非常感谢我最亲爱的朋友非常感谢你的重播它解决了。另外,你知道用简单的方法在桌子周围创建边框的技巧吗。我已经读到我必须在可绘制的地方创建xml,但这有点奇怪,你知道最简单的方法吗。再次感谢您的帮助。@MiranBCM谢谢您。但我担心,在stackoverflow中不欢迎在后面附加另一个问题。如果你想解决这个问题,请。。。尽管通过将表格放在框架布局中(带有背景和填充),它还是可以实现的。谢谢我解决了问题。非常感谢我最亲爱的朋友