我使用本教程http://developer.android.com/resources/tutorials/views/hello-tabwidget.html创建标签,打开新的意图。但是,教程中的选项卡位于顶部,所以我在这里按照此解决方案https://stackoverflow.com/a/2710404/301584将选项卡移动到底部(基本上我只是将TabWidget移动到xml文件中的FrameLayout之后,修改必要的layout_height并添加layout_weight,如链接上的解决方案所示。问题是,当我这样做时,我的应用程序总是被迫关闭。这是logcat报告的错误
01-09 04:30:09.838: ERROR/AndroidRuntime(336): FATAL EXCEPTION: main
01-09 04:30:09.838: ERROR/AndroidRuntime(336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.geoflex.trymasak/com.geoflex.trymasak.TryMasaktTabActivity}: java.lang.ClassCastException: android.widget.FrameLayout
01-09 04:30:09.838: ERROR/AndroidRuntime(336): Caused by: java.lang.ClassCastException: android.widget.FrameLayout
01-09 04:30:09.838: ERROR/AndroidRuntime(336): at com.geoflex.trymasak.TryMasaktTabActivity.onCreate(TryMasaktTabActivity.java:34)
这是我的完整代码
package com.geoflex.trymasak;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;
public class TryMasaktTabActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, Tab1.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tabOne");
spec.setContent(intent);
spec.setIndicator("Tab One");
tabHost.addTab(spec);
// Squish the tab a little bit horizontally
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 40;
// Bump the text size up
LinearLayout ll = (LinearLayout) tabHost.getChildAt(0);
android.widget.TabWidget tw = (android.widget.TabWidget) ll.getChildAt(0);
RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
TextView lf = (TextView) rllf.getChildAt(1);
lf.setTextSize(20);
// Do the same for the other tabs
intent = new Intent().setClass(this, Tab2.class);
spec = tabHost.newTabSpec("tabTwo");
spec.setContent(intent);
spec.setIndicator("Tab Two");
tabHost.addTab(spec);
tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 40;
RelativeLayout rlrf = (RelativeLayout) tw.getChildAt(1);
TextView rf = (TextView) rlrf.getChildAt(1);
rf.setTextSize(20);
tabHost.setCurrentTab(0);
}
}
和我的main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android: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">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TabWidget
android:id="@android:id/tabs"
android:layout_weight="0"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
和我的tab1.java(tab2.java是一样的)
package com.geoflex.trymasak;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Tab1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("This is tab 1");
setContentView(tv);
}
}
答案 0 :(得分:1)
尝试此操作在底部设置标签:
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_main"
>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@android:id/tabs"
/>
</RelativeLayout>
</TabHost>
使用以下代码进行主要活动(这会扩展TabActivity):
public class TabTestActivity extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this,Tab1.class);
spec = tabHost.newTabSpec("projects").setIndicator("Projects",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this,Tab2.class);
spec = tabHost.newTabSpec("news").setIndicator("News",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
}
答案 1 :(得分:0)
试试这个
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:paddingBottom="@dimen/tab_space_top"
android:layout_width="fill_parent" android:paddingLeft="@dimen/tab_space_gap"
android:layout_height="fill_parent" android:paddingRight="@dimen/tab_space_gap" >
<LinearLayout android:id="@+id/tab_relative_layout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background">
<FrameLayout android:id="@android:id/tabcontent" android:layout_weight="1"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_below="@android:id/tabs"></FrameLayout>
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0"></TabWidget>
</LinearLayout>
答案 2 :(得分:0)
RelativeLayout rllf = (RelativeLayout) tw.getChildAt(0);
我认为根据您的日志,这应该被转换为Framelayout。
答案 3 :(得分:0)
设置为片段
if(getActivity() != null && isAdded) {
//do your operation
}