1

我试图创建一个底部tabhost +片段。当我点击tabhost时,标签内容不会改变

的问题是,

1)当我按下标签按钮,它不会切换到另一个片段,虽然我的日志显示,该公司已经在创建视图输入。我厌倦了放在每个片段上的文本视图,但没有显示,所以我怀疑片段没有添加framelayout id:realtabcontent?

2)底部的标签似乎主机“太底”,使得一些标签的底部 的是在屏幕

的如何解决这些问题?感谢您的帮助

主要活动

public class MainActivity extends FragmentActivity { 
    public FragmentTabHost tabHost; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tabHost = (FragmentTabHost) findViewById(R.id.tabhost); 
     tabHost.setup(this, getSupportFragmentManager(), R.id.tabcontent); 
     tabHost.addTab(
       setIndicator(this, tabHost.newTabSpec("About"), 
         R.drawable.ic_launcher), About.class, null); 
     tabHost.addTab(
       setIndicator(this, tabHost.newTabSpec("Camera"), 
         R.drawable.ic_launcher), CameraHandle.class, null); 
     tabHost.addTab(
       setIndicator(this, tabHost.newTabSpec("Gallery"), 
         R.drawable.ic_launcher), PhotoGallery.class, null); 
     tabHost.addTab(
       setIndicator(this, tabHost.newTabSpec("LeaderBoard"), 
         R.drawable.ic_launcher), LeaderBoard.class, null); 
    } 

    public TabSpec setIndicator(Context ctx, TabSpec spec, int resId) { 
     // TODO Auto-generated method stub 
     View v = LayoutInflater.from(ctx).inflate(R.layout.custom_tab, null); 
     ImageView logo = (ImageView) v.findViewById(R.id.tab_logo); 
     logo.setImageResource(resId); 
     TextView text = (TextView) v.findViewById(R.id.tab_title); 
     text.setText(spec.getTag()); 
     return spec.setIndicator(v); 
    } 

} 

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <FrameLayout 
     android:id="@+id/realtabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="0dip" 
     android:layout_weight="1" /> 

    <android.support.v4.app.FragmentTabHost 
     android:id="@+id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <FrameLayout 
      android:id="@+id/tabcontent" 
      android:layout_width="0dp" 
      android:layout_height="0dp" 
      android:layout_weight="0" /> 
    </android.support.v4.app.FragmentTabHost> 

</LinearLayout> 

片段(对于每个选项卡一个片段)

public class About extends Fragment { 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.about, container, false); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
    } 

} 

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextVsssiew" /> 

</LinearLayout> 

回答

1

您对主要活动的布局有问题 - 它的高度为0px。您可以使用来自Android的工具的hierarchyviewer,找出其显示在您的应用程序的意见,一些细节 - 详细内容见:(http://developer.android.com/tools/debugging/debugging-ui.html

尝试以下方法,并从那里:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <android.support.v4.app.FragmentTabHost 
     android:id="@+id/tabhost" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:orientation="horizontal" /> 

    <FrameLayout 
     android:id="@+id/tabcontent" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

</LinearLayout> 
+0

但该选项卡是在布局的顶部,但没有布局的底部? – user782104

1

您应该尝试使用like this进行布局,以在屏幕底部设置选项卡。

<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:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:padding="5dp" > 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:padding="5dp" /> 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="-4dp" 
     android:layout_weight="0" /> 
</LinearLayout> 

相关问题