android fragment学习6--FragmentTabHost底部布局

本文分享了一个关于Android开发中使用Fragment与TabLayout的具体实践案例,通过实例代码展示了如何实现带有自定义Tab指示器的FragmentTabHost,并解决了相关布局警告问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我个人觉得这个与fragment4 5是一样的,但是之前有三个项目中就是这样用的,没有出现问题,有必要拿出来分享下。

我的博客
android fragment学习5–fragment扩展 TabLayout非自定义
https://blog.youkuaiyun.com/qq_26296197/article/details/81146989

涉及到原项目,就不放图片了。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/main_sildingmenu"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="@android:color/transparent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/fragment_tabcontent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </FrameLayout>
    <!-- 导航条-->
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"/>
    <android.support.v4.app.FragmentTabHost
        android:id="@+id/fragment_tabhost"
        android:layout_width="match_parent"
        android:layout_height="56dp">
    </android.support.v4.app.FragmentTabHost>
</LinearLayout>

这个布局会有警告,可以用这个方法解决(我没有进行优化,警告仍然正常跑项目,现在既然把代码贴上来,把解决办法写下)

<!--定义FragmentTabHost控件-->
    <android.support.v4.app.FragmentTabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/black" ><!--装4个Fragment-->

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" /><!--装Tab的内容-->
    </android.support.v4.app.FragmentTabHost>
import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.ainisi.myn.form.R;
import com.ainisi.myn.form.base.App;
import com.ainisi.myn.form.fragment.GroupChatFragment;
import com.ainisi.myn.form.fragment.HomePagerFragment;
import com.ainisi.myn.form.fragment.InfroPagerFragment;
import com.ainisi.myn.form.fragment.PersonalPagerFragment;
import com.ainisi.myn.form.model.AppField;
import com.ainisi.myn.form.model.Friends;
import com.ainisi.myn.form.server.broadcast.BroadcastManager;
import com.ainisi.myn.form.utils.BackGround;
import com.ainisi.myn.form.utils.BadgeUtils;
import com.ainisi.myn.form.utils.GetToken;
import com.ainisi.myn.form.utils.L;
import com.igexin.sdk.PushManager;
import java.util.List;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.UserInfo;
import me.leolin.shortcutbadger.ShortcutBadger;

public class MainActivity extends AppCompatActivity  {
    //测试
    private FragmentTabHost tabHost;
    private Object token;
    private List<Friends> userIdList;
    public TextView tvNum = null;
    public TextView tvNumInfro ;
    private static final String TAG = "MainActivity";
    private Bundle bundle;
    private long lastClickTime = 0;
    private String trueName;
    private String infromation;
    private String quickName;
    private String chooseName;
    private String head;
    private String choiceHead;
    public int messageNum;
    private Boolean isopenPush ;
    private Intent intent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    //咨询消息
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        intentJump(intent);
    }

    private void initView() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = getWindow();
            // Translucent status bar
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        intentJump(null);
        tabHost = (FragmentTabHost) findViewById(R.id.fragment_tabhost);
        //关联碎片
        tabHost.setup(this, getSupportFragmentManager(), R.id.fragment_tabcontent);
        //添加碎片
        tabHost.addTab(tabHost.newTabSpec("shouye").setIndicator(getView(0)), HomePagerFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("jiedan").setIndicator(getView(1)), PersonalPagerFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("pingjia").setIndicator(getView(2)), InfroPagerFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec("wode").setIndicator(getView(3)), GroupChatFragment.class, null);
        //去掉间隔图片
        tabHost.getTabWidget().setDividerDrawable(null);
        //群聊天
        if(intent!=null&&intent.getStringExtra("tofeedback")!=null){
            tabHost.setCurrentTab(3);
        }
        //资讯消息
        if(infromation!=null&&infromation.equals("yes")){
            tabHost.setCurrentTab(2);
        }
    }
    private void initData() {
        PushManager.getInstance().initialize(this.getApplicationContext());  //个推
        //把userToken给后台
        String loginUrId = App.mCache.getAsString(AppField.loginUserId);
        if (loginUrId != null) {
            getToken(loginUrId);
        }
        //注册广播.判读网络情况
        IntentFilter filter = new IntentFilter();
        filter.addAction(AppField.pushActionHide);
        filter.addAction(AppField.bgSendMessageAction);
        filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        filter.addAction(AppField.sendbadgetNum);
        registerReceiver(receiver, filter);
    }
    //得到视图 ,关联对应的tabhost
    private View getView(int position) {
        View v=null;
        ImageView tabImg = null;
        TextView tabText = null;
        if(position==3){
            v = LayoutInflater.from(this).inflate(R.layout.layout_tab_feedback, null);
            tabImg = (ImageView) v.findViewById(R.id.tab_img);
            tvNum = (TextView) v.findViewById(R.id.iv_num);
            tabText = (TextView) v.findViewById(R.id.tab_name);
        } else if(position==2){
            v = LayoutInflater.from(this).inflate(R.layout.layout_tab_infromation, null);
            tabImg = (ImageView) v.findViewById(R.id.tab_img);
            tvNumInfro = (TextView) v.findViewById(R.id.iv_num_infro);
            tabText = (TextView) v.findViewById(R.id.tab_name);
        }else {
            v = LayoutInflater.from(this).inflate(R.layout.layout_tab, null);
            tabImg = (ImageView) v.findViewById(R.id.tab_img);
            tabText = (TextView) v.findViewById(R.id.tab_name);
        }
        switch (position) {
            case 0:
                tabImg.setBackgroundResource(R.drawable.tab_select0);
                tabText.setText(R.string.homepager);
                break;
            case 1:
                tabImg.setBackgroundResource(R.drawable.tab_select1);
                tabText.setText(R.string.personal);
                break;
            case 2:
                tabImg.setBackgroundResource(R.drawable.tab_select2);
                tabText.setText(R.string.consult);
                break;
            case 3:
                tabImg.setBackgroundResource(R.drawable.tab_select3);
                tabText.setText(R.string.feedback);
                break;
        }
        return v;
    }
    }
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/tab_img"
        android:layout_width="34dp"
        android:layout_height="34dp"
        android:layout_gravity="center_horizontal" 

        />

    <TextView
        android:id="@+id/tab_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="12"

        />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="true" android:drawable="@mipmap/icon_home_press"/>
  <item android:state_selected="false" android:drawable="@mipmap/icon_home"/>
</selector>

已经给出主要的代码了,其他的代码就不贴上了。

参考文档
https://developer.android.google.cn/reference/android/support/v4/app/FragmentTabHost

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值