2012-02-05 79 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