Android 动态设置新文本视图的XML属性

Android 动态设置新文本视图的XML属性,android,android-layout,dynamically-generated,Android,Android Layout,Dynamically Generated,在我的应用程序中,我有一个按钮(Add),它接受输入的名称,用该名称创建一个TextView,并将TextView放置在线性布局中。您应该能够创建所需的科目(学校科目)数量,因此需要在Java代码中设置属性(如果我错了,请纠正我)。我已经设法创建了TextViews,但是它们有默认设置。如何使它们像第一个TextView?我还没有找到任何解决方案在这一个,也没有在Android开发者网站 以下是线性布局和第一个文本视图的XML: <LinearLayout android:id="

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

以下是
线性布局
和第一个
文本视图
的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>

提前谢谢

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

在这里,您可以找到它的工作原理和使用方法:

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();
            }
        }
    });