0

我正在尝试制作有滚动视图的动态布局。在滚动视图中,我添加了引用 main.xml 的垂直线性布局。代码立即停止,我尝试添加引用 main.xml 的布局。

 ScrollView scroll;
    EditText search;
    Button checkin;
        LinearLayout vt;

        public void onCreate(Bundle savedInstanceState) 
        {

            super.onCreate(savedInstanceState);
            scroll=new ScrollView(getApplicationContext());
            vt=(LinearLayout) findViewById(R.id.llv);///referencing linear layout located in main.xml.This is where problem the  is occurring.
            //vt.setOrientation(LinearLayout.VERTICAL);
            scroll.addView(vt);
            this.setContentView(scroll);
        }

main.xml(R.id.llv 所在的位置)

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



    <LinearLayout
        android:id="@+id/llv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
         <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

        <ToggleButton
            android:id="@+id/toggleButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ToggleButton" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

</LinearLayout>
4

3 回答 3

1

尝试添加

  setContentView(R.layout.main); 

前:

  vt=(LinearLayout) findViewById(R.id.llv);
于 2012-05-14T11:36:47.993 回答
0

选中此项以动态创建线性布局。

http://www.dreamincode.net/forums/topic/130521-android-part-iii-dynamic-layouts/

于 2012-05-14T12:01:00.767 回答
0

尝试这个 :

view row  = LayoutInflater.from(this).inflate(R.layout.main, null);
LinearLayout layout=row.findViewById(R.id.YourLayout);
于 2012-05-14T11:48:13.137 回答