2014-09-24 2 views
1

Вот мой макет activity_main.xml, который содержит две вкладки. Выбранное содержимое вкладки вообще не отображается. Где я ошибаюсь? Содержимое вкладки отображается, если я заменяю места FragmentTabHost и Framelayout в макете ниже. I. Создание вкладок отображается внизу. Но я хочу, чтобы мои вкладки были в верхней части экрана. Пожалуйста, помогите мне.Содержимое вкладок FragmentTabHost не отображается

<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="@android:id/tabhost" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

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

</LinearLayout> 

MainActivity

public class MainActivity extends FragmentActivity { 
private FragmentTabHost mTabHost; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main); 

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); 

    LayoutInflater inflator = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 
    View v1 = inflator.inflate(R.layout.tab_bg, null); 
    TextView tv1 = (TextView)v1.findViewById(R.id.tabTitleTextView); 
    tv1.setText("Tab1"); 

    View v2 = inflator.inflate(R.layout.tab_bg, null); 
    TextView tv2 = (TextView)v2.findViewById(R.id.tabTitleTextView); 
    tv2.setText("Tab2"); 

    mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator(v1), FragmentTab.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator(v2), FragmentTab.class, null); 
    mTabHost.setCurrentTab(0);  
} 

} 

FragmentTab:

public class FragmentTab extends Fragment { 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment_layout, null); 
    TextView tv = (TextView) v.findViewById(R.id.text); 
    tv.setText(this.getTag() + " Content"); 
    return v; 
} 
} 
+0

пост Java файл также –

+0

@MehulJoisar: отредактировали вопрос – user1670443

ответ

4

попробовать это:

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

<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" 
     android:layout_weight="0" 
     android:orientation="horizontal" /> 

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

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

и изменить

mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); 

к

mTabHost.setup(this, getSupportFragmentManager(),R.id.realtabcontent); 
+0

Я не могу поверить, что это зафиксировано, что дерьмовый проблема, я столкнулся на API 16 устройств – vanomart

Смежные вопросы