我正在尝试从TabView
TextView
添加文字,但获取java.lang.IllegalStateException
活动代码:
public class Testimonial extends TabActivity {
private TabHost tabHost;
TextView video = null;
TextView text = null;
private void setupTabHost() {
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testimonial);
setupTabHost();
View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.testimonial_tab_row, null);
video = (TextView)view.findViewById(R.id.tabVideo);
text = (TextView)view.findViewById(R.id.tabText);
tabHost = getTabHost();
TabSpec videoSpec = tabHost.newTabSpec("Video");
videoSpec.setIndicator(video);
Intent videoIntent = new Intent(this,TestimonialVideo.class);
videoSpec.setContent(videoIntent);
TabSpec textSpec = tabHost.newTabSpec("Text");
textSpec.setIndicator(text);
Intent textIntent = new Intent(this, TestimonialText.class);
textSpec.setContent(textIntent);
tabHost.addTab(videoSpec);
tabHost.addTab(textSpec);
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabdrawable);
}
tabHost.getTabWidget().setDividerDrawable(R.drawable.footer_saprater);
}
}
这是我的testimonial.xml代码
<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">
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="@android:id/tabs"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/tabdrawable"
android:gravity="center_vertical|center_horizontal" />
</RelativeLayout>
</TabHost>
这里是testimonial_tab_row.xml代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:gravity="center"
android:orientation="vertical">
<TextView android:id="@+id/tabVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Video"
android:gravity="center"
android:textSize="22sp"
android:textColor="#fff" />
<TextView android:id="@+id/tabText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:gravity="center"
android:textSize="22sp"
android:textColor="#fff" />
</LinearLayout>
答案 0 :(得分:0)
错误显示video
且text
已有父母。由于您是从同一ViewGroup
检索它,因此它们包含在LinearLayout
/ FrameLayout
/ RelativeLayout
内,因此它们有父级。
答案 1 :(得分:0)
而不是使用布局,您可以从代码本身添加textview
TextView text=new TextView(this);
text.setText()
text.setGravity()
text.setTextColor()
TextView video=new TextView(this);
video.setText()
video.setGravity()
video.setTextColor()
答案 2 :(得分:0)
try this
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabdrawable);
}
tabHost.getTabWidget().setDividerDrawable(R.drawable.footer_saprater);
tabHost.removeAllViews();