2017-08-24 38 views
0

我正在尝试在XML中创建一个TabLayout作为用户配置文件页面。实质上,我想在3个不同的选项卡上显示文字,文字将根据Firebase中的动态数据进行更新。这是我的代码,它不是呈现我的第一个选项卡。我基本上是瞄准了如下设计:在渲染过程中提出无法在XML中创建TabLayout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/tabcontent"> 

    <TabHost android:id="@+id/tab_host" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 
      <TabWidget android:id="@android:id/tabs" 

       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" > 
       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="TEST"/> 
       </TabWidget> 
    </TabHost> 

</FrameLayout> 

enter image description here

回答

0

例外:TabHost需要与 ID的FrameLayout里 “机器人:ID/tabcontent”。

这意味着你应该使用的FrameLayout的TabHost内,上述ID

0

你可以尝试新的android.support.design.widget.TabLayout,简单易用。这里是一个简单的例子,点击TabItem切换页面。

布局

<android.support.design.widget.TabLayout 
    android:id="@+id/main_tablayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:tabIndicatorHeight="4dp" 
    app:tabGravity="fill"> 

    <android.support.design.widget.TabItem 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="table 1"/> 

    <android.support.design.widget.TabItem 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="table 2"/> 

    <android.support.design.widget.TabItem 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="table 3"/> 
</android.support.design.widget.TabLayout> 

<ViewAnimator 
    android:id="@+id/main_viewanimator" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/main_recyclerview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

    <Button 
     android:id="@+id/main_button_pip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="@string/pip_mode"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="TEST"/> 
</ViewAnimator> 

的Java代码

mViewAnimator = findViewById(R.id.main_viewanimator); 
mTabLayout = findViewById(R.id.main_tablayout); 
mTabLayout.addOnTabSelectedListener(mTabListen); 

private final TabLayout.OnTabSelectedListener mTabListen = new TabLayout.OnTabSelectedListener() { 
    @Override 
    public void onTabSelected(TabLayout.Tab tab) { 
     switch (tab.getPosition()) { 
      case 0: 
       mViewAnimator.setDisplayedChild(0); 
       break; 

      case 1: 
       mViewAnimator.setDisplayedChild(1); 
       break; 

      case 2: 
       mViewAnimator.setDisplayedChild(2); 
       break; 
     } 
    } 

    @Override 
    public void onTabUnselected(TabLayout.Tab tab) { 
    } 

    @Override 
    public void onTabReselected(TabLayout.Tab tab) { 
    } 
}; 

快照 enter image description here

+0

而我认为你的答案可能会工作,我主要使用选项卡作为XML和我要去的地方动态文本在选项卡内(它将根据与Firebase的交互进行更新)。我实际上并不需要recylerview功能,这些标签仅仅显示与配置文件屏幕相似的内容。 – tccpg288