如何在单击按钮时膨胀另一个 xml
我有一个 Activity
,因为没有 Button
,但我已经以这样的方式对其进行了编码:另一个 Activity
出现,现在就在这一秒Activity
有三个 Button
,当我单击所有按钮时,会展开不同的相应活动。 在第二个 Activity
中,我又添加了一个 Button
,单击此第四个 Button
后,我不需要另一个 Activity
code> 进入前台,而不是我只想膨胀另一个布局(我不想更改 Activity
) 在该布局(我需要膨胀)中,我有一个 ListView
,其中包含某些站点的列表,当此布局膨胀时,应显示站点列表,当我单击该站点时,它应该带我去那个网页。
问题 1: 如何在单击第四个 Button
时仅膨胀 xml
而不更改 Activity
。
问题2: 当 xml
膨胀时,如果我单击 BackButton
,则应显示包含所有 4 个 Button
的 Activity
。
这是我用来膨胀另一个 xml
的代码:
fourthButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
callingMore();
}
});
private void callingMore() {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.web_site_list_xml, (ViewGroup)findViewById(R.id.site_list));
}
但是此代码强制关闭,并且没有 xml
被膨胀,如果我使用 setContentView( R.layout.web_site_list_xml)
在 callingMore()
方法中 xml
被夸大了,但是 xml
没有显示任何内容,然后如果我点击 BackButton
,Activity
未显示全部 4 个 Button
。
I have one Activity
in that there are no Button
s but I have coded it in such a way that another Activity
comes, now in this second Activity
there are three Button
s, on which when I click all inflating different respective activities.
In this second Activity
there I have added one more Button
, on clicking this 4th Button
I don't want another Activity
to come in foreground instead i just want another layout to be inflated(I don't wanna change the Activity
)
In that layout(which i need to inflate) I have a ListView
which has a list of certain sites, when this layout is inflated the list of sites should be shown and as i click on there sites its should take me to that webpage.
PROBLEM 1:
How to inflate just the xml
on click of the 4th Button
without changing the Activity
.
PROBLEM 2:
When the xml
is inflated and if I click on BackButton
the Activity
containing all the 4 Button
s should be displayed.
Here is the code that I'm using to inflate another xml
:
fourthButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
callingMore();
}
});
private void callingMore() {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.web_site_list_xml, (ViewGroup)findViewById(R.id.site_list));
}
BUT this code gives a force close and no xml
is being inflated and if I use setContentView( R.layout.web_site_list_xml)
inside callingMore()
method
the xml
is inflated but that xml
doesn't show anything and then if I hit BackButton
, the Activity
that has all 4 Button
s isn't shown.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我尝试了这段代码..希望对你有帮助..
1.layout_existed.xml
2.layout_toinfliate.xml
3.LayoutInflateDemo.java
I tried this code..hope helps to you..
1.layout_existed.xml
2.layout_toinfliate.xml
3.LayoutInflateDemo.java
尝试使用 v.findViewByID()..
将 View v 传递给 CallingMore()
Try using v.findViewByID()..
Pass View v to callingMore()