简体   繁体   English

AlertDialog中的Android TabActivity

[英]Android TabActivity in AlertDialog

How to display TabActivity in AlertDialog? 如何在AlertDialog中显示TabActivity? I know that it's a little bit tricky, as we should run TabActivity as normal activity, 我知道这有点棘手,因为我们应该将TabActivity作为正常活动运行,

eg 例如

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

where Settings - TabActvivity. 设置-TabActvivity。

The only way I know it to set view for AlertDialog, but it will don't work. 我知道它是为AlertDialog设置视图的唯一方法,但是它不起作用。

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

Is there any way to show TabActivity in AlertDialog? 有什么方法可以在AlertDialog中显示TabActivity?

Thanks, 谢谢,

You're sort of conflating the concept of Views and Activities here, but probably the easiest way to do what you want is to set your TabActivity 's theme to Theme.Dialog and start it, instead of using an AlertDialog and trying to wrap an Activity inside a popup inside another Activity . 您在这里将“视图和活动”的概念混为一谈,但是可能要做的最简单的方法是将TabActivity的主题设置为Theme.Dialog并启动它,而不是使用AlertDialog并尝试包装一个Activity另一个里面一个弹出内Activity For your own sanity's sake, don't go down that road into Inception-type territory. 为了您自己的理智,不要沿着那条路走到盗梦空间类型的领域。

I wont really recommend this approach but since you have requested the behavior heres a sample code: 我不会真的推荐这种方法,但是由于您已请求行为,因此这里是示例代码:

Heres a Tab layout , which has a EditText in tab1 and a Button in tab2 这是一个Tab 布局 ,该布局tab1中具有一个EditText ,在tab2中具有一个Button

<?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>

Code to inflate this layout to a AlertDialog 将这个布局膨胀到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();

As I said THIS IS NOT RECOMMENDED APPROACH create a theme instead as suggested by Yoni Samlan above. 正如我所说的那样, 不建议使用这种方法 ,请按照上面的Yoni Samlan的 建议创建一个主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM