Android Java:自动向主布局添加子布局,构建可滚动多行多列表格
问题描述
我想要创建一个可垂直和水平滚动的表格。已有如下主布局和MainActivity类,该类可创建包含多个TextView的新LinearLayout。当前使用for循环时,新的TextView会被添加到同一行中,但我希望生成例如10行5列(每行含5个TextView)的结构。我曾考虑使用嵌套循环的解决方案,但这样需要创建新布局,又遇到了findById的问题——我无法在XML中创建例如100个新布局。
现有MainActivity代码
package v1.projectTech; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TableLayout; import android.widget.TextView; public class MainActivity3 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); createOneRow(); } public void createLayouts() { int tmpnumberOfRows = MainActivity2.temp3.size(); String[] numberOfLayouts = new String[5]; for (int i = 0; i < 5; i++) { LinearLayout linearLayout3_1_in = findViewById(R.id.layout4_1); } } public void createOneRow() { // int tmpNumberOfColumns = InformacjaOLokalizacji.numberOfColumns; LinearLayout linearLayout3_1_in = findViewById(R.id.layout3_1); LinearLayout linearLayout4_1_in = findViewById(R.id.layout4_1);//find the linear layout linearLayout4_1_in.removeAllViews(); //add this too for (int i = 0; i < 5; i++) { //looping to create 5 textviews TextView textView = new TextView(this); //dynamically create textview textView.setLayoutParams(new LinearLayout.LayoutParams( //select linearlayoutparam- set the width & height ViewGroup.LayoutParams.WRAP_CONTENT, 40)); textView.setGravity(Gravity.FILL_VERTICAL); //set the gravity textView.setText("Textview: " + i); //adding text linearLayout4_1_in.addView(textView); //inflating :) } } }
现有XML布局代码
<?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="fill_parent" android:layout_height="fill_parent" android:id="@+id/layout3" tools:context=".MainActivity3" android:orientation="horizontal"> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/layout3_1" android:orientation="vertical" > <!-- row1--> <LinearLayout android:layout_width="wrap_content" android:layout_height="40dp" android:id="@+id/layout4_1" android:orientation="horizontal" > </LinearLayout> </LinearLayout> </ScrollView> </HorizontalScrollView> </LinearLayout>
解决方案
不需要在XML里预先创建所有行布局,直接通过代码动态生成每行的LinearLayout,再添加到垂直容器中即可,具体实现如下:
1. 修改XML布局
移除XML中预先写的layout4_1行布局,只保留最外层的垂直容器layout3_1:
<?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" android:id="@+id/layout3" tools:context=".MainActivity3" android:orientation="horizontal"> <HorizontalScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/layout3_1" android:orientation="vertical" > <!-- 行布局全部由代码动态生成 --> </LinearLayout> </ScrollView> </HorizontalScrollView> </LinearLayout>
2. 修改MainActivity代码
用嵌套循环动态生成多行多列结构,无需通过findViewById获取动态布局:
package v1.projectTech; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity3 extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); createTable(10, 5); // 生成10行5列的表格 } // 动态生成表格,参数为行数和列数 public void createTable(int rowCount, int columnCount) { LinearLayout verticalContainer = findViewById(R.id.layout3_1); verticalContainer.removeAllViews(); // 清空原有内容 // 外层循环生成每行 for (int row = 0; row < rowCount; row++) { // 创建当前行的水平LinearLayout LinearLayout rowLayout = new LinearLayout(this); rowLayout.setLayoutParams(new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, 40)); rowLayout.setOrientation(LinearLayout.HORIZONTAL); // 内层循环生成每行的TextView for (int col = 0; col < columnCount; col++) { TextView textView = new TextView(this); textView.setLayoutParams(new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)); textView.setGravity(Gravity.CENTER); // 文字居中提升可读性 textView.setText(String.format("行%d列%d", row+1, col+1)); textView.setPadding(10, 0, 10, 0); // 增加左右内边距避免文字拥挤 rowLayout.addView(textView); } // 将生成的行添加到垂直容器中 verticalContainer.addView(rowLayout); } } }
关键说明
- 动态生成的行布局直接通过代码引用操作,不需要提前在XML中定义ID。
- 调整
createTable方法的参数即可灵活生成任意行数和列数的表格。 - 优化了TextView的显示样式,提升表格整体可读性。
内容的提问来源于stack exchange,提问作者Remek
相关产品推荐
相关产品推荐

