2010-12-15 84 views
1

如何在AlertDialog中显示TabActivity? 我知道这是有点棘手,因为我们应该运行TabActivity作为正常的活动,AlertDialog中的Android TabActivity

例如,

Intent intent = new Intent(MyClass.this, Settings.class); 
startActivity(intent); 

其中设置 - TabActvivity。

我知道它设置AlertDialog视图的唯一方法,但它不会工作。

View view = (View) ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)) 
    .inflate(R.layout.settings, null); 
new AlertDialog.Builder(this).setTitle("AlertDialog").setView(view).show(); 

有没有什么办法可以在AlertDialog中显示TabActivity?

感谢,

回答

4

你有点混为一谈的意见和活动的概念,在这里,但可能是最简单的方式做你想要的是你的TabActivity的主题设置为Theme.Dialog并启动它,而不是使用AlertDialog并试图将Activity包装在另一个Activity内的弹出窗口内。为了你自己的理智,不要沿着这条路进入初始型领域。

3

我不会真的建议这种做法,但既然你已经要求继承人的行为的示例代码:

继承人标签布局,其中有一个EditTextTAB1ButtonTAB2

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/tabhost" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent">  
<LinearLayout 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 

<TabWidget android:id="@android:id/tabs" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/> 

<FrameLayout android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout android:id="@+id/tab1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:layout_centerHorizontal="true" 
    > 
    <EditText 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="TextBox" 
    /> 

    </LinearLayout> 
<Button android:id="@+id/tab2" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:text="A semi-random button" 
/> 
</FrameLayout></LinearLayout></TabHost> 

代码膨胀此布局的AlertDialog

AlertDialog.Builder builder=new AlertDialog.Builder(this); 
    LayoutInflater inflator=getLayoutInflater(); 
    View view=inflator.inflate(R.layout.main, null); 

    TabHost tabHost=(TabHost)view.findViewById(R.id.tabhost); 
    tabHost.setup(); 
    TabHost.TabSpec spec=tabHost.newTabSpec("tag1"); 
    spec.setContent(R.id.tab1); 
    spec.setIndicator("Clock"); 
    tabs.addTab(spec); 

    spec=tabHost.newTabSpec("tag2"); 
    spec.setContent(R.id.tab2); 
    spec.setIndicator("Button"); 
    tabs.addTab(spec); 

    builder.setView(view); 
    builder.setPositiveButton("OK", null); 
    builder.create().show(); 

正如我所说这不是推荐的方法通过上述号Yoni Samlan创建主题,而不是作为建议。

+0

如果我有一个充气器视图,那么我如何将ID传递给setContent? – chhameed 2014-03-04 14:02:13