Android:如何结合膨胀布局和非膨胀布局?

时间:2012-10-31 14:55:08

标签: android layout inflate

在我的行为中,我想在顶部显示一些文本视图,然后在它下面展开列表视图。我已尝试各种方法但没有成功,例如addview()但无法使其工作。我的膨胀视图成为主要布局。

非常感谢任何帮助。 基于Ted Hopp解决方案的LogCat错误

10-20 02:38:38.322:D / AndroidRuntime(1253):关闭VM 10-20 02:38:38.322:W / dalvikvm(1253):threadid = 3:线程退出未捕获异常(组= 0x4001b188) 10-20 02:38:38.322:E / AndroidRuntime(1253):未捕获的处理程序:由于未捕获的异常导致线程主要退出 10-20 02:38:38.342:E / AndroidRuntime(1253):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.mycoursetimetable / com.example.mycoursetimetable.MyCourses}:java.lang.IllegalStateException:不能将标题视图添加到列表 - 已调用setAdapter。 10-20 02:38:38.342:E / AndroidRuntime(1253):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496) 10-20 02:38:38.342:E / AndroidRuntime(1253):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 10-20 02:38:38.342:E / AndroidRuntime(1253):在android.app.ActivityThread.access $ 2200(ActivityThread.java:119) 10-20 02:38:38.342:E / AndroidRuntime(1253):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1863) 10-20 02:38:38.342:E / AndroidRuntime(1253):在android.os.Handler.dispatchMessage(Handler.java:99) 10-20 02:38:38.342:E / AndroidRuntime(1253):在android.os.Looper.loop(Looper.java:123) 10-20 02:38:38.342:E / AndroidRuntime(1253):在android.app.ActivityThread.main(ActivityThread.java:4363) 10-20 02:38:38.342:E / AndroidRuntime(1253):at java.lang.reflect.Method.invokeNative(Native Method) 10-20 02:38:38.342:E / AndroidRuntime(1253):at java.lang.reflect.Method.invoke(Method.java:521) 10-20 02:38:38.342:E / AndroidRuntime(1253):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:860) 10-20 02:38:38.342:E / AndroidRuntime(1253):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 10-20 02:38:38.342:E / AndroidRuntime(1253):at dalvik.system.NativeStart.main(Native Method) 10-20 02:38:38.342:E / AndroidRuntime(1253):引起:java.lang.IllegalStateException:无法将标题视图添加到列表 - 已调用setAdapter。 10-20 02:38:38.342:E / AndroidRuntime(1253):在android.widget.ListView.addHeaderView(ListView.java:256) 10-20 02:38:38.342:E / AndroidRuntime(1253):在android.widget.ListView.addHeaderView(ListView.java:279) 10-20 02:38:38.342:E / AndroidRuntime(1253):at com.example.mycoursetimetable.MyCourses.onCreate(MyCourses.java:30) 10-20 02:38:38.342:E / AndroidRuntime(1253):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 10-20 02:38:38.342:E / AndroidRuntime(1253):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) 10-20 02:38:38.342:E / AndroidRuntime(1253):... 11更多 10-20 02:38:38.382:I / dalvikvm(1253):threadid = 7:对信号3作出反应 10-20 02:38:38.382:E / dalvikvm(1253):无法打开堆栈跟踪文件'/data/anr/traces.txt':权限被拒绝 10-20 02:38:47.463:I / Process(1253):发送信号。 PID:1253 SIG:9

activity_my_courses

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/main" >

    <Button
        android:id="@+id/labelAddCourseButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="addCourseButton"
        android:padding="10dp"
        android:text="@string/CourseName" />

</LinearLayout>

listcourses

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >



    <TextView
        android:id="@+id/labelModuleCode"
        android:layout_height="wrap_content"
         android:layout_width="wrap_content"
         android:layout_marginLeft="10dp"
        android:text="@string/CourseName"
        android:textSize="10dp" />

     <TextView
        android:id="@+id/labelCourseType"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/CourseType"
        android:layout_marginLeft="10dp"
        android:textSize="10dp" />
</LinearLayout>

代码

public class MyCourses extends ListActivity {

    static final String TEST = "com.example.mycoursetimetable.TEST";
    String [] MODULE;

        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
        super.onCreate(savedInstanceState);

        LinearLayout main = (LinearLayout)this.findViewById(R.id.main);
        MODULE = getResources().getStringArray(R.array.module);
        setListAdapter(new ListArrayAdapter(this,MODULE));
        }



        public void addCourseButton (View addCourseButton) 
        {
         Intent intent = new Intent(this,AddCourse.class);
         startActivity(intent);
        }

        protected void onListItemClick(ListView l, View v, int position, long id)
        {
          super.onListItemClick(l, v, position, id);


        try {
            Class test = Class.forName("com.example.MyCourseTimeTable.AddCourse");
            Intent intent = new Intent(MyCourses.this, test);

            TextView textView = (TextView) v.findViewById(R.id.labelModuleCode);
            String module = textView.getText().toString();


            intent.putExtra(TEST,module);
            startActivity(intent);
            }   
                catch (ClassNotFoundException e)
                {
                e.printStackTrace();
                }

        }

}

class ListArrayAdapter extends ArrayAdapter<String> 
{
    //  You only need one copy of LayoutInflater
    private final LayoutInflater inflater;

    //create the ArrayAdpater
    public ListArrayAdapter(Context context, String[] test) 
    {
        super(context, R.layout.activity_my_courses, test);
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ViewHolder holder;
        if(convertView == null) {


            // Inflate the layout that you want for each row


            convertView = inflater.inflate(R.layout.listcourses, parent, false);

            holder = new ViewHolder();
            holder.code = (TextView) convertView.findViewById(R.id.labelModuleCode);
            holder.type = (TextView) convertView.findViewById(R.id.labelCourseType);

            // Add a link for your button as well and define its OnClickListener here to, eventually...

            convertView.setTag(holder);
        }
        else 
            holder = (ViewHolder) convertView.getTag();

        String module = getItem(position);
        //set the text to the string values based on position
        holder.code.setText(module);

        //return the layout
        return convertView;
    }

    class ViewHolder {
        TextView code;
        TextView type;
    }
}

2 个答案:

答案 0 :(得分:3)

您可以使用以下内容:

,而不是尝试定义自己的活动布局
public class MyCourses extends ListActivity {

    static final String TEST = "com.example.mycoursetimetable.TEST";
    String [] MODULE;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        MODULE = getResources().getStringArray(R.array.module);
        getListView().addHeaderView(getHeader()); // must come before next line
        setListAdapter(new ListArrayAdapter(this,MODULE));
    }

    private View getHeader() {
        LayoutInflater inflater =
            (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        return inflater.inflate(R.layout.listcourses, null);
    }
    . . .
}

这将使layout/listcourses.xml中的视图定义为列表的标题。

答案 1 :(得分:0)

我猜你需要在xml文件中有一个<ListView/>以下的文本视图android id应该是默认的android:id\list其次你需要setContentView(YOUR LAYOUT FILE)中的onCreate()