Android为多个膨胀文本视图设置文本仅在最后一个视图上有效

Android为多个膨胀文本视图设置文本仅在最后一个视图上有效,android,textview,layout-inflater,Android,Textview,Layout Inflater,我是Android新手,正在尝试制作一个简单的清单应用程序。我正在测试使用textview的xml模板将textview动态添加到活动中。问题是,当我尝试膨胀多个视图时,它会创建文本视图,但只将文本设置为上次创建的视图 以下是活动中的代码: LinearLayout linearLayout = (LinearLayout)findViewById(R.id.main_linear_layout); TextView textView1 = (TextView)getLayou

我是Android新手,正在尝试制作一个简单的清单应用程序。我正在测试使用textview的xml模板将textview动态添加到活动中。问题是,当我尝试膨胀多个视图时,它会创建文本视图,但只将文本设置为上次创建的视图

以下是活动中的代码:

    LinearLayout linearLayout = (LinearLayout)findViewById(R.id.main_linear_layout);

    TextView textView1 = (TextView)getLayoutInflater().inflate(R.layout.textview_template, linearLayout).findViewById(R.id.textview_template);
    TextView textView2 = (TextView)getLayoutInflater().inflate(R.layout.textview_template, linearLayout).findViewById(R.id.textview_template);
    TextView textView3 = (TextView)getLayoutInflater().inflate(R.layout.textview_template, linearLayout).findViewById(R.id.textview_template);

    textView1.setText("Take out trash.");
    textView2.setText("Wash windows.");
    textView3.setText("Why won't you work?");
以下是我尝试将文本视图插入的主要布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/main_linear_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:clipToPadding="false"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#EEEEEE"
            android:elevation="4dp"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:elevation="2dp"
        android:background="#FFFFFF">
        <EditText android:id="@+id/new_task_text_edit"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:hint="@string/text_edit_hint"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_add"
            android:onClick="addTask"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="4dp">

    </LinearLayout>

</LinearLayout>


我尝试在每次充气后但在下一次充气前设置文本,但结果相同。我做错了什么?

事实上,你所有的
文本视图1
文本视图2
文本视图3
都指向同一个用户界面,因为你正在使用相同的id进行充气,并且你正在立即搜索


Id必须是不同的,为了更好地进行编码,请使用
recyclerview
及其适配器,因为上面的编码可能会更好。

实际上,您所有的
textView1
textView2
textView3
都指向同一个UI,因为你正在使用相同的id进行充气,并且你正在立即搜索

Id必须是不同的,为了更好地编码,请使用
recyclerview
及其适配器,因为上面的编码可以更好。

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview_template"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="6dp"
        android:paddingBottom="6dp"
        android:textColor="#000000"
        android:textSize="20dp"
        android:background="@drawable/item_box"
 />
textview_template.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textview_template"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingTop="6dp"
        android:paddingBottom="6dp"
        android:textColor="#000000"
        android:textSize="20dp"
        android:background="@drawable/item_box"
 />

我最初是这样做的,但Android Studio在编译时出现错误,说如果textview没有嵌套在某种布局中,它就无法解析textview_template.xml文件。你可以按自己的方式来做,这并不重要,重要的是,在膨胀remove findById时,不要传递linearlayout参数@QwurticusI最初是这样做的,但Android Studio在编译时出现错误,说如果textview没有嵌套在某种布局中,它无法解析textview_template.xml文件。你可以按自己的方式来做,这并不重要,重要的是,在膨胀remove findById时,不要通过linearlayout argument@qwurticus,只要在谷歌网站上说一个关于这个的教程,我就去看看。谢谢。你可以看到这个,作为一个例子)只要在谷歌的网站上说一个关于这个的教程,我就去看看。谢谢。你可以看到这个,作为一个例子)
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.main_linear_layout);
 TextView textView1 = (TextView) getLayoutInflater().inflate(R.layout.textview_template, null);
 TextView textView2 = (TextView) getLayoutInflater().inflate(R.layout.textview_template, null);
 TextView textView3 = (TextView) getLayoutInflater().inflate(R.layout.textview_template, null);

textView1.setText("Take out trash.");
textView2.setText("Wash windows.");
textView3.setText("Why won't you work?");
linearLayout.addView(textView1);
linearLayout.addView(textView2);
linearLayout.addView(textView3);