初次使用FragmentTabHost(v4下面的)出现的问题

来源:互联网 发布:汉字域名有价值吗 编辑:程序博客网 时间:2024/05/18 10:02

第一次使用FragmentTabHost做主tabhost布局


问题1:直接xml布局FragmentTabHost,不写后台代码,程序会异常

问题2:tabhost.addTab(tabSpec,fragment[i], null);,原来tabhost是tabhost.addTab(tabSpec),而fragment中需要制定具体的fragment对象


正确的配置:

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.demo.ui.HomeActivity"
    tools:ignore="MergeRootFrame" >
    
    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@android:id/tabhost"
        android:layout_alignParentTop="true"/>
        
    
    <android.support.v4.app.FragmentTabHost
   android:id="@android:id/tabhost"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_alignParentBottom="true"
   android:background="@drawable/tab_bg_up">
   <!-- 
  <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" /> 
            -->
</android.support.v4.app.FragmentTabHost>
         
    
</RelativeLayout>


code:

package com.demo.ui;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;


import com.demo.R;
import com.demo.ui.fragment.ContactsFragment;
import com.demo.ui.fragment.DynamicFragment;
import com.demo.ui.fragment.MessageFragment;


public class HomeActivity extends FragmentActivity /*implements OnTabChangeListener*/{


private String tag = "HomeActivity";
private HomeActivity context;
private FragmentTabHost tabhost;
private LayoutInflater inflater;
private Class fragment[] = {MessageFragment.class,ContactsFragment.class,DynamicFragment.class};
private int image[] = {R.drawable.tab_message_selector,R.drawable.tab_contacts_selector,R.drawable.tab_dynamic_selector};
private String title[] = {"消息","联系人","动态"};
private int currentTabIndex = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
context = HomeActivity.this;
init();

}


private void init() {
initObj();
initView();
}




private void initObj() {
inflater = LayoutInflater.from(context);
}

private void initView() {
tabhost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabhost.setup(context, getSupportFragmentManager(), R.id.realtabcontent);
//循环初始化
for(int i=0;i<fragment.length;i++){
TabSpec tabSpec = tabhost.newTabSpec(title[i]).setIndicator(getTabView(i));
tabhost.addTab(tabSpec,fragment[i], null);
//给每一个tab设置背景
//tabhost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tab_bg_selector);
}
//设置监听
//tabhost.setOnTabChangedListener(context);
//tabhost.setCurrentTab(currentTabIndex);
}


//获得每一个tab的View
public View getTabView(int index){
View view = inflater.inflate(R.layout.tab_view, null);
ImageView iv_icon = (ImageView)view.findViewById(R.id.iv_icon);
//iv_icon.setImageResource(image[index]);
TextView tv_title = (TextView)view.findViewById(R.id.tv_title);
// tv_title.setText(title[index]);
// if(index==2){
// TextView tv_hint = (TextView)view.findViewById(R.id.tv_hint);
// tv_hint.setVisibility(View.VISIBLE);
// tv_hint.setText("4");
// }
return view;
}



//tab切换
// @Override
// public void onTabChanged(String title) {
// Log.i(tag, "切换tab:"+title);
// currentTabIndex = tabhost.getCurrentTab();
// Log.i(tag, "切换tab,currentTabIndex:"+currentTabIndex);
// }



}

0 0
原创粉丝点击