使用java将xml小部件添加到选项卡视图

时间:2013-05-29 12:13:53

标签: java android xml widget

我一直在关注本指南,并让它运行。 http://www.mkyong.com/android/android-tablayout-example/

我已经使用java(图像视图和文本字段)向标签添加了一堆新的小部件,但它变得凌乱,我没有尽可能多的控制定位和大小,因为我不能使用布局

如果我使用xml创建了一个新布局,并且我使用布局等完美地放置了所有小部件,那么我是否可以使用java在该布局中填充特定选项卡?

例如 而不是像这样的代码添加小部件

TextView textview = new TextView(this);
textview.setText("This is Android tab");
setContentView(textview);

我可以添加对xml文件的引用,它会在xml文件中添加所有小部件

1 个答案:

答案 0 :(得分:3)

使用LayoutInflater http://developer.android.com/reference/android/view/LayoutInflater.html

LayoutInflater inflater = LayoutInflater.from(context); // if you're inside of activity then context = this
View viewInflatedFromXml = inflater.inflate(R.layout.your_layout, null);

然后你可以用这个观点做你想做的事。