Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.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:从模板文件创建TextView并附加到现有TextView_Android_Textview - Fatal编程技术网

Android:从模板文件创建TextView并附加到现有TextView

Android:从模板文件创建TextView并附加到现有TextView,android,textview,Android,Textview,我有一个简单文本视图的模板,simple_txt.xml: <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="30dp" android:textSize="18sp" &g

我有一个简单文本视图的模板,
simple_txt.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:textSize="18sp" >
</TextView>

我有一个主布局。我还有一些其他的文本视图:

...
<TextView
    android:id="@+id/main_text_view"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
/> 
...
。。。
...
因此,我需要从template
simple_txt.xml
创建一些文本视图,并将其附加到*main_text_view*(simple_txt在main_text_view之后)

我知道可以将任何视图添加到布局(addView),但它会在布局内部添加视图,而不是在其旁边


如何执行此操作?

将文本视图包装到其他布局中,例如LinearLayout。使用LayoutFlater创建新的文本视图,然后将它们插入到围绕原始文本视图的布局中。例如:

<LinearLayout android:id="@+id/textViewContainer" 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="vertical">
    <TextView
        android:id="@+id/main_text_view"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        /> 
</LinearLayout>

在android中重用UI尝试在Google中搜索ViewGroup和LinearLayout llMain=(LinearLayout)findViewById(R.id.textViewContainer)的方法之间的区别;按钮btnNew=新按钮(此按钮);llMain.addView(btnNew)??问题:我是否可以从模板文件创建视图,对其进行处理(设置文本…),然后生成addView()?我使用ViewGroup,因为它是支持所需方法的最通用类型。这允许您将资源文件中的LinearLayout更改为其他类型的视图组,而无需更改相应的代码。是的,如果您存储了“充气”的结果,则可以在添加文本之前设置文本。
ViewGroup group = ((ViewGroup)findViewById(R.id.textViewContainer));
group.addView (
     LayoutInflater.from(this).inflate(R.layout.simple_text, group));