2012-02-05 78 views
0

原諒我簡單的問題,因爲我是新來的android。 :)Android TextView文本沒有顯示在標籤內?

爲什麼textview的text屬性不顯示在此選項卡內?

屏幕:Here

這裏是我的代碼..

MapTab2.java

package com.test.maptab2; 

import android.app.TabActivity; 
import android.os.Bundle; 
import android.widget.TabHost; 

public class MapTab2 extends TabActivity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TabHost.TabSpec spec; 

     spec = getTabHost().newTabSpec("tab1"); 
     spec.setContent(R.id.mapstub); 
     spec.setIndicator("Map"); 
     getTabHost().addTab(spec); 

     spec = getTabHost().newTabSpec("tab2"); 
     spec.setContent(R.id.detailstub); 
     spec.setIndicator("Detail"); 
     getTabHost().addTab(spec); 

     getTabHost().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"> 

     <!-- Tab-switch panel --> 
     <TabWidget android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/> 

     <!-- Tab contents --> 
     <FrameLayout android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 

      <!-- Map here --> 
      <TextView android:id="@+id/mapstub" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="map"/> 

      <!-- Other stuff --> 
      <TextView android:id="@+id/detailstub" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="detail"/> 

     </FrameLayout> 
    </LinearLayout> 
</TabHost> 

我試圖建立我的方式達到在選項卡中顯示地圖。最好先理解基礎知識,我想。 :')

回答

1

我實際上有更多的運氣在父視圖(在這種情況下,你的LinearLayout)設置引力,然後在子視圖(在你的情況下,你的TextViews等)設置layout_gravity屬性。爲了得到這個簡單的例子,我將所有的「fill_parent」屬性改爲「wrap_content」(或者作爲另一種替代方法,使用0.0dp) - 然後你可以開始使用不同的屬性來玩一切,喜歡。

+0

謝謝,將所有內容設置爲wrap_content使文本顯示。 – Ephemeros 2012-02-05 14:17:28

0

嘗試在Tab中的每個TextView中添加android:gravity =「center」。

+0

那沒有工作不幸 – Ephemeros 2012-02-05 12:58:28

+0

它沒有?如何在java代碼中使用[this](http://developer.android.com/reference/android/widget/TextView.html#setGravity(int))? – 2012-02-05 13:04:23

+0

但是無論如何,無論我改變xml中的重力屬性還是間接改變java,都可以得到相同的結果? – Ephemeros 2012-02-05 13:53:24