我想通过在我的Android应用程序中按一个按钮来引入新的布局。这是代码片段:
Button button = (Button)findViewById(R.id.btnAccept);
button.setOnClickListener(new View.OnClickListener() {
LayoutInflater layoutInflater = LayoutInflater.from(getBaseContext());
View promptView = layoutInflater.inflate(R.layout.empty_layout, null);
public void onClick(View v) {
// What should I write here to prompt empty_layout?
}
}
我不知道如何使用“promptView”。你能给我一些见解吗?
谢谢你们!
答案 0 :(得分:2)
如果是屏幕的整个视图,请使用
setContentView(prompView);
如果它只是视图的一部分,那么使用
yourPortionViewContainer.addView(prompView);
或者你可以使用片段。
答案 1 :(得分:1)
你应该创建Intent来打开新的Activity或创建Fragment并让FragmentManager显示它。
答案 2 :(得分:0)
我不知道我是否正确理解了您的问题,但是按下按钮时添加布局的方法是按照以下方式进行编程:
LinearLayout mainLayout;
mainLayout = (LinearLayout)findViewById(R.id.mainLayout)//assuming u gave the main layout an id of mainLayout in your XML file
public void onClick(View v) {
LinearLayout layout = new LinearLayout(this);
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);//create variable to store layout parameters
layout.setOrientation(LinearLayout.VERTICAL);//set orientation of layout
layout.setLayoutParams(params);//set layout parameters
mainLayout.addView(layout);//add the newly created layout to the already existing layout
}