将listview放在RelativeLayout中时,它总是在寻找R.id.list

时间:2015-06-10 01:12:36

标签: android android-layout listview android-listview

我试图在相对布局中放置一个imageview和一个listview,但我总是遇到这样的错误

Your content must have a ListView whose id attribute is 'android.R.id.list'

然后我尝试将ListView作为父级并且它可以工作,但我想在列表视图上方显示图像。

这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/header"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/header" />

<ListView
    android:id="@+id/list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >
</ListView>

这是我的片段

public class Tab_About extends ListFragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.tab_about, container,
            false);

    Resources res = getResources();

    final String[] values = res.getStringArray(R.array.about_list);
    Integer imageId = R.drawable.accessory_arrow;

    ListView list = (ListView)rootView.findViewById(R.id.list);

    CustomListAdapter cus = new CustomListAdapter(getActivity(),values, imageId);
    list.setAdapter(cus);


    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Context context = getActivity().getApplicationContext();
            CharSequence text = "You clicked " + position;
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }
    };

    // Setting the item click listener for the listview
    list.setOnItemClickListener(itemClickListener);

    return rootView;
}
}

我该怎么办?谢谢你的帮助

1 个答案:

答案 0 :(得分:1)

它要求 android:id =&#34; @id / android:list&#34; 的原因是因为你使用ListFragment扩展你的Fragment,请阅读这里的文档  ListFragment Documentation

您只需使用Fragment而不是ListFragment扩展Fragment类。