Java Eclipse,图像视图上视图中的布局

Java Eclipse,图像视图上视图中的布局,java,android,eclipse,view,android-inflate,Java,Android,Eclipse,View,Android Inflate,所以我有一门课叫book。这本书既是一个java类,也是一个用于布局的xml文件。然后我有一个名为book_shelf的类/xml文件。在我的book_shelf xml文件中,我有一个名为book1的视图 public class book_shelf extends Activity { @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds it

所以我有一门课叫book。这本书既是一个java类,也是一个用于布局的xml文件。然后我有一个名为book_shelf的类/xml文件。在我的book_shelf xml文件中,我有一个名为book1的视图

public class book_shelf extends Activity {
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.mainmenu, menu);

        book MyBook = new book();
        final View book1 = (View) findViewById(R.id.book1);
        book1.(load my book xml layout in this view)
        return true;

    }

}

我想将书籍布局加载到书架框架的视图中。请,谢谢。

您需要使用充气机为所需布局充气,然后在视图中添加布局。 例如

编辑: 试试这个


book1.addView(MyBook);非常感谢,但是book1.addView(bookView)给出了一个错误。我注意到addView仅适用于视图组tho。我尝试将book1转换为视图组,但没有成功。((视图组)book1).添加视图(bookView);检查已编辑的:零件。如果其中一个零件也会崩溃并出现相同错误,则视图不能强制转换到视图组。也许我做错了。是否有可以添加多个视图的元素“ViewGroup”而不是“View”?例如,我的书架将是ViewGroup类型,它可以容纳许多书的视图?好吧,所以我有点明白了。问题是我不能将它添加到视图中,我需要将它添加到布局中,在我的例子中是FrameLayout。最终FrameLayout book1=(FrameLayout)findViewById(R.id.book1);但它不会将书本缩小到框架布局的大小。
book MyBook = new book();
final View book1 = (View) findViewById(R.id.book1);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//assuming R.layout.book your book xml
View bookView = inflater.inflate(R.layout.book, null);

//Add bookView layout to your book1.
book1.addView(bookView)
final View book1 = (View) findViewById(R.id.book1);
((ViewGroup) book1).addView(bookView, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));