我想在自定义对话框中使用标签。
这是我得到的:
[project_settings.xml]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#ffffffff"
android:layout_height="450dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:id="@+id/text_dialog"
android:layout_below="@+id/a"
android:layout_marginTop="20dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="20dp"
android:textSize="18sp"
android:textColor="#ff000000"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="OK"
android:id="@+id/btn_dialog"
android:gravity="center_vertical|center_horizontal"
android:layout_below="@+id/text_dialog"
android:layout_marginBottom="20dp"
android:background="@android:color/holo_green_dark"
android:layout_centerHorizontal="true"
android:textColor="#ffffffff" />
<ImageView
android:layout_width="match_parent"
android:id="@+id/a"
android:gravity="center"
android:background="#DA5F6A"
android:scaleType="fitCenter"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:layout_height="100dp" />
<TabHost
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_height="match_parent"
android:id="@+id/tabhost">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="44dp">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/general"
android:text="OK"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/resources"
android:text="OK"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/variables"
android:text="OK"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
[ProjectSettings.java]
public class ProjectSettingsWindow {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.project_settings);
// create the TabHost that will contain the Tabs
TabHost tabHost = (TabHost)dialog.findViewById(R.id.tabhost);
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");
TabHost.TabSpec tab2 = tabHost.newTabSpec("Second Tab");
TabHost.TabSpec tab3 = tabHost.newTabSpec("Third tab");
tab1.setIndicator("Tab1");
tab1.setContent(new Intent(""));
tab2.setIndicator("Tab2");
tab2.setContent(new Intent(""));
tab3.setIndicator("Tab3");
tab3.setContent(new Intent(""));
tabHost.addTab(tab1);
tabHost.addTab(tab2);
tabHost.addTab(tab3);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
如何制作原生的工作标签?为什么它只显示XML?
我在执行java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TabWidget.addView(android.view.View)' on a null object reference
时遇到tabhost.addTab()
错误,即使存在此类ID的布局也是如此。
答案 0 :(得分:1)
在添加标签之前,您需要设置()tabHost!
尝试添加以下代码行:
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
// create the TabHost that will contain the Tabs
TabHost tabHost = (TabHost)dialog.findViewById(R.id.tabhost);
tabHost.setup(mLocalActivityManager);
TabHost.TabSpec tab1 = tabHost.newTabSpec("First Tab");