【发布时间】:2012-06-17 06:08:07
【问题描述】:
我有一个扩展View 的类View1。我想在这个类View1 中膨胀R.layout.test2.xml。我在这个类中放了以下代码
public class View1 extends View {
View view;
String[] countries = new String[] {"India", "USA", "Canada"};
public View1( Context context) {
super(context);
LayoutInflater mInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=mInflater.inflate(R.layout.test2, null, false);
}
}
从另一个类Home 我希望这个膨胀的视图在某些情况下存在,在Home 类中我编写了以下代码:
public class Home extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
CreateView();
}
public void CreateView() {
LinearLayout lv=(LinearLayout)findViewById(R.id.linearlayout);
View1 view = new View1(Home.this);
lv.addView(view);
}
}
但是当我运行我的项目时,活动并没有显示任何东西。
【问题讨论】:
标签: android view android-inflate