java - TabHost.TabSpec content not appearing -
i have tabhost in 1 of activity layouts 4 tabspecs. have set content each tabspec different linearlayout, first tabspec displays linearlayout. have tried many things still can't figure out what's wrong. here's code:
public class conversionactivity extends activity { phconversion phc = new phconversion(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_conversions); tabhost th = (tabhost) findviewbyid(r.id.tabhost); th.setup(); tabspec spec1 = th.newtabspec("tab 1"); spec1.setcontent(r.id.tab1); spec1.setindicator("ph"); tabspec spec2 = th.newtabspec("tab 2"); spec2.setindicator("temp"); spec2.setcontent(r.id.tab2); tabspec spec3 = th.newtabspec("tab 3"); spec3.setcontent(r.id.tab3); spec3.setindicator("length"); tabspec spec4 = th.newtabspec("tab 4"); spec4.setcontent(r.id.tab4); spec4.setindicator("volume"); th.addtab(spec1); th.addtab(spec2); th.addtab(spec3); th.addtab(spec4);
and xml code:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:gesture-image="http://schemas.polites.com/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".conversionactivity" > <linearlayout android:id="@+id/linearlayout1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:orientation="horizontal" > <linearlayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="8" android:orientation="vertical" > </linearlayout> </linearlayout> <tabhost android:id="@+id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignparentleft="true" android:layout_alignparenttop="true" > <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" > </tabwidget> <framelayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <linearlayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/given" android:textsize="32sp" > </textview> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ph" android:textsize="20sp" > </textview> <checkbox android:id="@+id/phbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="disableothersph" > </checkbox> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/poh" android:textsize="20sp" > </textview> <checkbox android:id="@+id/pohbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="disableotherspoh" > </checkbox> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/h" android:textsize="20sp" > </textview> <checkbox android:id="@+id/hbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="disableothersh" > </checkbox> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/oh" android:textsize="20sp" > </textview> <checkbox android:id="@+id/ohbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onclick="disableothersoh" > </checkbox> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" > <edittext android:id="@+id/phconvinput" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="5" android:inputtype="numberdecimal" > </edittext> <button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:onclick="solveph" android:text="@string/phsolvebutton" > </button> </linearlayout> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/phsolution" android:textsize="32sp" > </textview> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ph" android:textsize="20sp" > </textview> <textview android:id="@+id/phanswer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/somespace" android:textsize="20sp" > </textview> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/poh" android:textsize="20sp" > </textview> <textview android:id="@+id/pohanswer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/somespace" android:textsize="20sp" > </textview> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/h" android:textsize="20sp" > </textview> <textview android:id="@+id/hanswer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/somespace" android:textsize="20sp" > </textview> </linearlayout> <linearlayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/oh" android:textsize="20sp" > </textview> <textview android:id="@+id/ohanswer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/somespace" android:textsize="20sp" > </textview> </linearlayout> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/phsolution" android:textsize="32sp" > </textview> <imageview android:id="@+id/what" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/phschem" /> </linearlayout> <linearlayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textsize="32sp" > </textview> </linearlayout> <linearlayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </linearlayout> <linearlayout android:id="@+id/tab4" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > </linearlayout> </linearlayout> </framelayout> </linearlayout> </tabhost>
i realize tabs 3 , 4 empty tab 2 still appears blank when click it, tab 1 shows correct linearlayout.
in short, linearlayout
s tab1, tab2, , tab3 children of tab1 linearlayout
. your tabs should siblings of each other.
here other items might in layout well:
- i'm not sure why
linearlayout
android:id="@+id/linearlayout1"
exists, i'd remove it; - you don't need use
android:layout_alignparentleft
orandroid:layout_alignparenttop
intabhost
because matching parent's width , height, i'd remove attributes; - you're using
android:layout_width="fill_parent"
onimageview
. instead, should usematch_parent
becausefill_parent
deprecated. see http://developer.android.com/reference/android/view/viewgroup.layoutparams.html#attr_android:layout_height saysfill_parent
:
the view should big parent (minus padding). constant deprecated starting api level 8 , replaced match_parent.
Comments
Post a Comment