Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/17.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 - Fatal编程技术网

Android 如何对相对布局进行充气

Android 如何对相对布局进行充气,android,Android,我有一个任务来扩展XML视图。我真的不知道该怎么办。我 使用基本适配器 @Override public View getView(int position, View convertView, ViewGroup parent) { //TODO - Get the current ToDoItem final ToDoItem toDoItem = mItems.get(position); //TODO - Inflate the View for this

我有一个任务来扩展XML视图。我真的不知道该怎么办。我 使用基本适配器

@Override
public View getView(int position, View convertView, ViewGroup parent) {


    //TODO - Get the current ToDoItem
    final ToDoItem toDoItem =  mItems.get(position);

    //TODO - Inflate the View for this ToDoItem
    // from todo_item.xml.

      RelativeLayout itemLayout =(RelativeLayout)convertView.findViewById(R.id.RelativeLayout1);
     View InflatedView= null;
todo_item.xml看起来像

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView>
  <!-- some code -->
</TextView>

</RelativeLayout>

但这不起作用。我也不理解传递给getview方法的convertView。

ListViewAPI有一些古怪之处

getView()
中提供给您的
convertView
参数实际上可以为空。这就是
ListView
如何在用户滚动列表时循环视图。因此,您不能使用它来为您的
相对值膨胀

要获取视图,应首先检查
convertView
是否为空。如果是,则需要使用提供给您的
parent
参数对
视图进行充气

RelativeLayout itemLayout;

if (convertView != null) {
  itemLayout = (RelativeLayout) convertView;
} else {
  LayoutInflater inflater = LayoutInflater.from(parent.context);
  itemLayout = (RelativeLayout) inflater.inflate(R.layout.todo_item, parent, false);
}
RelativeLayout itemLayout;

if (convertView != null) {
  itemLayout = (RelativeLayout) convertView;
} else {
  LayoutInflater inflater = LayoutInflater.from(parent.context);
  itemLayout = (RelativeLayout) inflater.inflate(R.layout.todo_item, parent, false);
}