我在文件 view_inflated.xml 下有一个带有以下 xml 标记的简单 TextView:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Inflated view"
>
</TextView>
我想在文件 activity_main.xml 下声明的 LinearLayout 中膨胀 TextView,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
现在为了膨胀我尝试这样的视图:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resource=new String[]{"Arafat","Anik","Prottoy","Progga","Prapti",
"Oishy","Nijha","Adil","Troyee","Kushum","Manik","Shahin","Limon"
,"Shumon","Lipi","Lucy","Shaikot"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,resource);
lv=(ListView) findViewById(R.id.listView1);
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
LayoutInflater inflater=getLayoutInflater();
View v=inflater.inflate(R.layout.view_inflated, null);
LinearLayout rl=(LinearLayout) findViewById(R.id.ll1);
rl.addView(v);
}
问题是 - 列表视图按预期显示,但 TextView 没有出现。如何解决这个问题?