如何在main.xml中插入list.xml文件

时间:2012-11-20 06:25:08

标签: android xml android-listview

有人帮我做下面的事! 我想在main.xml文件的底部插入一些list list.xml文件。如何实现呢?

   ----------------------------
    |    main.java              |
    |    main.xml               |
    |                           |
    |                           |
    |                           |
    |                           |
    |      ______               |
    |     |button|              |
    |                           |
    |                           |
    |                           |
    | _________________________ | 
    | |      list.java        | |
    | |      list.xml         | |
    | |                       | |
    | |                       | |
    | |                       | |
    | |                       | |
    | |                       | |
    |_|_______________________|_| 

I called list.java by using intent on the main.java .I also include list.xml to main.xml. When I press button list.xml should pop up like above figure. But list.xml comes up new window. How to solve this problem?

3 个答案:

答案 0 :(得分:2)

你可以尝试这样......

我的示例代码......

<RelativeLayout
    android:id="@+id/xK1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="vertical"
    >

    <include
        android:id="@+id/your id"// your id
        layout="@layout/list" >// add the list.xml here
    </include>
</RelativeLayout>

添加到您的Main.xml。

答案 1 :(得分:2)

尝试以下代码并根据您的要求进行一些更改。这只是指南代码。使用布局充气程序服务。假设您有2个主文件,然后列出

public class Test extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    LinearLayout layoutMain = new LinearLayout(this);
    layoutMain.setOrientation(LinearLayout.HORIZONTAL);
    setContentView(layoutMain);
    LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(R.layout.main, null);
    RelativeLayout layoutRight = (RelativeLayout) inflate.inflate(R.layout.list, null);

    RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutMain.addView(layoutLeft, 100, 100);
    layoutMain.addView(layoutRight, relParam);
}
}

答案 2 :(得分:1)

由于您想重新使用布局,即在其他布局中包含现有布局,我建议您阅读本文:Re-using Layouts with <include/>

现在您要在main.xml中包含list.xml,在main.xml中编写<include/>,如下所示:

 <include layout="@layout/list"/>