如何在充气点击按钮另一个XML [英] How to inflate another xml on clicking a button
问题描述
我有一个活动
中不存在任何按钮
取值,但我有codeD就在这样的一种方式,另一种活动
来了,现在在这个第二个活动
有三个按钮
S,哪个当我点击所有的充气各自不同的活动。
在那里,我已经增加了一个按钮
,上单击此4 按钮<本次
活动/ code>我不希望再
活动
进来前景,而不是我只是想另一个布局被夸大(我不想改变活动
)
在这种布局(我需要充气)我有一个的ListView
具有一定的站点列表,在此布局充气应显示的站点列表,并为我点击网站有其应该带我到那个网页。
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.
问题1:
如何膨胀只是 XML
4号按钮
不改变活动的点击code>。
问题2:
当 XML
膨胀,如果我点击后退按钮
的活动
包含所有4 按钮
取值应显示。
PROBLEM 2:
When the xml
is inflated and if I click on BackButton
the Activity
containing all the 4 Button
s should be displayed.
下面是code,我使用充气另一个 XML
:
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));
}
但这种code给出了一个强制关闭,也没有 XML
正在膨胀,如果我使用的setContentView(R.layout.web_site_list_xml)
在 callingMore()
方法
在 XML
充气但 XML
不显示任何东西,然后如果我打返回按钮
的活动
具有所有4 按钮
s没有显示。
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.
推荐答案
喜阿里·我想这code..hope帮助你..
hi ali i tried this code..hope helps to you..
1.layout_existed.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/existedlayout"
>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Clicck to inflate view"
android:id="@+id/button"
/>
</LinearLayout>
2.layout_toinfliate.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/mytext"
android:text="You inflated me..i added to u r layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
3.LayoutInflateDemo.java
public class LayoutInflateDemo extends Activity {
private LinearLayout layoutToAdd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_existed);
layoutToAdd = (LinearLayout) findViewById(R.id.existedlayout);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = LayoutInflater
.from(getApplicationContext());
View view = inflater.inflate(R.layout.layout_toinfliate, null);
layoutToAdd.addView(view);
}
});
}
}
这篇关于如何在充气点击按钮另一个XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!