2

我在文件 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 没有出现。如何解决这个问题?

4

4 回答 4

1

不时解决你的问题,然后找出你错过了什么

android:orientation="vertical"

在你的activity_main.xml :)

这样做,你的问题就会得到解决。

要将 textview 设置在顶部,请将主布局替换为相对布局

<RelativeLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <ListView
        android:layout_alignParentBottom="true"
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="400dp"
       android:layout_gravity="bottom" >
    </ListView>

</RelativeLayout>

在代码中也进行类型转换,然后将视图添加到此相对布局,然后将适配器设置为列表。

像这样

LayoutInflater inflater = getLayoutInflater();
    View v = inflater.inflate(R.layout.view_inflated, null);
    RelativeLayout rl = (RelativeLayout) findViewById(R.id.ll1);
    rl.addView(v);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, resource);

    ListView lv = (ListView) findViewById(R.id.listView1);
    lv.setAdapter(adapter);
于 2013-10-15T04:57:28.720 回答
0

将您的膨胀视图作为页脚添加到列表视图。

setAdapter

 lv=(ListView) findViewById(R.id.listView1);
 LayoutInflater inflater=getLayoutInflater();
 View v=inflater.inflate(R.layout.view_inflated, null);
 lv.addFooterView(v);
 lv.setAdapter(adapter);
 lv.setOnItemClickListener(this);
于 2013-10-15T04:38:57.377 回答
0
// try this
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this, R.layout.view_inflated,resource);
于 2013-10-15T04:39:00.110 回答
0

您可以使用部分列表视图

在此处输入图像描述

您可以参考以下链接

http://amitandroid.blogspot.in/2013/05/android-section-listview.html

希望这有帮助。

于 2013-10-15T05:01:46.213 回答