所以我有一门叫做书的类(class)。本书既是一个java类,也是一个用于布局的xml文件。然后我有一个名为 book_shelf 的 class/xml 文件。在我的 book_shelf xml 文件中,我有一个名为 book1 的 View 。
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;
}
}
我想将书籍布局加载到书架框架的 View 中。请并谢谢您。
最佳答案
您需要使用充气器来充气所需的布局,然后将布局添加到 View 中。 例如
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));
关于java - Eclipse, ImageView 上的 View 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20778848/