Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中动态添加视图_Android_Android Layout_View - Fatal编程技术网

如何在Android中动态添加视图

如何在Android中动态添加视图,android,android-layout,view,Android,Android Layout,View,我在活动中有一个带页脚的列表视图。我想做的是以编程方式向页脚添加textview 有Activity.java,Activity.xml,footer.xml java的onCreate()方法的一部分: Activity.xml: <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientatio

我在活动中有一个带页脚的列表视图。我想做的是以编程方式向页脚添加textview

Activity.javaActivity.xmlfooter.xml

java的onCreate()方法的一部分:

Activity.xml:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <ListView
            android:id="@+id/referList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
 </LinearLayout>

footer.xml:

<LinearLayout
    android:id="@+id/footer_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center"
    xmlns:android="http://schemas.android.com/apk/res/android">
</LinearLayout>


但我在应用程序中找不到添加的文本视图。请有人帮忙。

尝试创建TextView实例并将其添加到ListView适配器

 ListView listView = .. //create your list, or inflate it from xml...
 TextView footerView = new TextView(activity.getApplicationContext());
 listView.addFooterView(footerView);
 listView.setAdapter(myAdapter);

您没有使用此
LinearLayout布局
对象,您发布的
footer.xml
与此无关,因为您正在调用
listView.addFooterView(footer)使用此
R.layout。请参阅页脚。
 ListView listView = .. //create your list, or inflate it from xml...
 TextView footerView = new TextView(activity.getApplicationContext());
 listView.addFooterView(footerView);
 listView.setAdapter(myAdapter);