在TabHost新布局 [英] New layouts in TabHost
问题描述
我在我的应用程序中使用tabHost但在一个视图(对应于标签之一)我有一个按钮,要带我到另一个活动,然后另一个布局。现在的问题是:我怎么得到这个新的布局可以继续访问该标签?或更好说了,我怎么加载的FrameLayout这里面新的布局?
I'm using tabHost in my application but in one of the views (corresponding to one of the tabs) I have a button that have to take me to another activity and then another layout. The question is: how do I get this new layout can continue to have access to the tabs? or better say, How do I load this new layout inside the FrameLayout ?.
在这里,我已上载我想要做的事:的http:/ /imageshack.us/photo/my-images/541/exampleu.png/
Here I have uploaded what I'm trying to do: http://imageshack.us/photo/my-images/541/exampleu.png/
在此先感谢!
PD:我在Android的是新的,也许是有没有更好的方式来实现我的purpouse不使用TabActivity。我接受任何建议。
Pd: I'm new in Android, maybe is there a better way to achieve my purpouse without using TabActivity. I'm open to any suggestion.
编辑:所以我决定用片段,我建议。现在,我有以下几点:
EDITED: so I decided to use Fragments as I was suggested. And now I have the following:
- AplicationActivity延伸FragmentActivity
- ClientActivity延伸片段
- SettingsActivity延伸片段
- DataClientActivity延伸片段
和以下布局:
- activity_aplicacion
- activity_client
- activity_settings
- activity_data_client
该activity_aplicacion.xml有TabHost,和的FrameLayout和TabWidget从这些我可以去ClientActivity使用SettingsActivity标签。
The activity_aplicacion.xml has TabHost, FrameLayout and TabWidget and from these I can go to ClientActivity and SettingsActivity using tabs.
在ClientActivity我称之为一个按钮,新,当我preSS这个按钮我想去
DataClientActivity。所以,在ClientActivity我也忒以下内容:
In ClientActivity I have a button called "new" and when I press this button I want to go to DataClientActivity. So, in ClientActivity I have te following:
public void onClickNew(View view){
DataClientActivity fragmentDataClient = new DataClientActivity ();
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.tabcontent,fragmentDataClient , "fragmentDataClient ");
ft.addToBackStack(null);
ft.commit();
}
但是当我运行我的应用程序,我得到了folling错误:
But when I run my app, I got the folling error:
21 05-04:55:04.780:E / AndroidRuntime(7515):java.lang.IllegalStateException:找不到在活动课com.nrAplicationActivity对视图类的Android的onClick处理程序的方法onClickNew(查看)。 widget.Button ID为buttonNew
05-04 21:55:04.780: E/AndroidRuntime(7515): java.lang.IllegalStateException: Could not find a method onClickNew(View) in the activity class com.n.r.AplicationActivity for onClick handler on view class android.widget.Button with id 'buttonNew'
所以,我有点迷惑分辩了。为什么我要在AplicationActivity的onClickNew方法,而不是在ClientActivity在那里我有按钮?
So I'm a little confuse rigth now. Why should I have the onClickNew method in AplicationActivity and not in ClientActivity where I have the button?
组织编写了:我发现这个解决方案:
EDITED 2: I found the solution for this:
公共类ClientActivity扩展片段{
public class ClientActivity extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.activity_clientes, container, false);
**// Register for the Button.OnClick event
Button b = (Button)view.findViewById(R.id.buttonNew);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Toast.makeText(Tab1Fragment.this.getActivity(), "OnClickMe button clicked", Toast.LENGTH_LONG).show();
Log.e("onClickNuevo2 ", "inicio");
DataClientActivity fragmentDataClient= new DataClientActivity();
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.tabcontent,fragmentDataClient, "fragmentDataClient");
ft.addToBackStack(null);
ft.commit();
}
});**
return view;
}
}
我只需要登记自己的ClientActivity里面的onClick监听到我的按钮。现在每个完美的作品!非常感谢迪夫亚Motiwala :)并感谢此链接:http://thepseudo$c$cr.word$p$pss.com/2011/10/04/android-tabs-the-fragment-way/#comment-410
I just needed to register the onClick listener to my button inside my ClientActivity. Now every works perfectly!. Thanks so much Divya Motiwala :) and thanks to this link: http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/#comment-410
推荐答案
您可以使用片段
,而不是内部的标签activites。并点击按钮,就可以用一个新的像这样的取代现有的片段:
You can use Fragments
instead of activites inside Tab. And on click of button, you can replace existing fragment with a new one like this :
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.realtabcontent,newFrag, "New Fragment");
ft.addToBackStack(null);
ft.commit();
在 ft.replace
第一个参数是的FrameLayout到片段进行连接,第二是片段类对象进行instatiated和第三与标签名。
In ft.replace
1st parameter is the frameLayout to which fragment is to be attached, second is the fragment class object to be instatiated and third is the tag name.
这篇关于在TabHost新布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!