[英]Android TabActivity in AlertDialog
如何在AlertDialog中显示TabActivity? 我知道这有点棘手,因为我们应该将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?
谢谢,
您在这里将“视图和活动”的概念混为一谈,但是可能要做的最简单的方法是将TabActivity
的主题设置为Theme.Dialog
并启动它,而不是使用AlertDialog
并尝试包装一个Activity
另一个里面一个弹出内Activity
。 为了您自己的理智,不要沿着那条路走到盗梦空间类型的领域。
我不会真的推荐这种方法,但是由于您已请求行为,因此这里是示例代码:
这是一个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>
将这个布局膨胀到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的 建议创建一个主题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.