动态设置新 TextView 的 XML 属性

Dynamically set XML properties of new TextViews

在我的应用程序中,我有一个按钮 (Add),它接受输入的名称,使用该名称创建一个 TextView 并将 TextView 放在 LinearLayout 中.您应该能够创建所需的科目(学校科目)数量,因此需要在 Java 代码中设置属性(如果我错了请纠正我)。我设法让它创建了 TextViews,但它们有默认设置。我怎样才能使它们像第一个 TextView 一样?我在这个问题上和 Android 开发者网站上都没有找到任何解决方案。

这是 LinearLayout 和第一个 TextView 的 XML:

<LinearLayout
    android:id="@+id/subjectLayout"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toTopOf="@+id/entryEditText"
    app:layout_constraintEnd_toStartOf="@+id/divider"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0">

    <TextView
        android:id="@+id/subjectMATHTextView"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Math"
        android:textAlignment="center"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/entryEditText"
        app:layout_constraintEnd_toStartOf="@+id/divider"
        app:layout_constraintHorizontal_bias="0.49"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.259" />

</LinearLayout>

和 Java 代码:

final ProgressBar subjectMATHProgressBar = (ProgressBar) findViewById(R.id.subjectMATHProgressBar);
    final EditText entryEditText = (EditText) findViewById(R.id.entryEditText);
    Button progressBtn = (Button) findViewById(R.id.progressBtn);
    final Button addSubjectButton = (Button) findViewById(R.id.addSubjectBtn);


    // For Progress Button
    progressBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                if (!entryEditText.getText().toString().matches("")) {
                    int currentProgress = subjectMATHProgressBar.getProgress();
                    int toSet = currentProgress + Integer.parseInt(entryEditText.getText().toString());
                    subjectMATHProgressBar.setProgress(toSet);
                }
            } catch (Exception e){
                Toast.makeText(getApplication().getBaseContext(), "Enter a number", Toast.LENGTH_SHORT).show();
            }
        }
    });


    final LinearLayout subjectLayout = (LinearLayout) findViewById(R.id.subjectLayout);
    LinearLayout progressLayout= (LinearLayout) findViewById(R.id.progressLayout);

    // For Add Subject Button
    addSubjectButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {

                // Important code:
                TextView newTextView = new TextView(getApplication().getBaseContext());
                newTextView.setText(entryEditText.getText().toString());
                subjectLayout.addView(newTextView);

            } catch (Exception e){
                Toast.makeText(getApplication().getBaseContext(), "Enter a valid name", Toast.LENGTH_SHORT).show();
            }
        }
    });

提前致谢!

这不是它的工作原理,如果我做对了,你需要列一个主题列表吗? 为此,有一个特殊的视图 - RecyclerView 或 ListView。它允许您使用数据列表填充您的布局。每个数据条目负责列表中的不同项目。

在这里您可以找到它的工作原理和使用方法: https://developer.android.com/guide/topics/ui/layout/recyclerview