消息关闭
    暂无新消息!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TabLayout
        android:id="@+id/mTabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:tabGravity="fill"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabMode="fixed"
        app:tabSelectedTextColor="@color/colorAccent"
        app:tabTextColor="@android:color/white"
        />

    <android.support.v4.view.ViewPager
        android:id="@+id/mViewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
       />


</LinearLayout>
package com.example.myapplication;


import android.support.v4.app.Fragment;
import android.support.design.widget.TabLayout;

import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.example.myapplication.fragment.ButlreFragment;
import com.example.myapplication.fragment.girlFragment;
import com.example.myapplication.fragment.userFragment;
import com.example.myapplication.fragment.weixinFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private List<String> tab;
    private List<Fragment> fragments;

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


    }

    private void initData() {
        tab = new ArrayList<>();
        tab.add("智能管家");
        tab.add("微信精选");
        tab.add("美女相册");
        tab.add("个人中心");
        fragments = new ArrayList<>();
        fragments.add(new ButlreFragment());
        fragments.add(new weixinFragment());
        fragments.add(new girlFragment());
        fragments.add(new userFragment());
    }

    private void initView() {
        mTabLayout = (TabLayout) findViewById(R.id.mTabLayout);
        mViewPager = (ViewPager) findViewById(R.id.mViewPager);
        //预加载
        mViewPager.setOffscreenPageLimit(fragments.size());
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            //设置标题
            @Override
            public CharSequence getPageTitle(int position) {
                return tab.get(position);
            }
     });
        mTabLayout.setupWithViewPager(mViewPager);
    }

}
package com.example.myapplication.fragment;

import android.support.v4.app.Fragment;import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.R;

/**
 * Created by xia on 2017/7/27.
 */

public class girlFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.girllayout,null);
        return view;
    }
}



找了好久没找到错误  求大神解决   不胜感激

4个回答

︿ 2
把android.support.design.widget.TabLayout  设置一个固定高度,然后 给每个fragment对应的xml 添加一个背景色试试看 
︿ 1
<?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:gravity="center">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="美女相册"/>

</LinearLayout>
这是xml文件。。。不知道怎么回复个人。。。
︿ 0
 View view=inflater.inflate(R.layout.girllayout,null);

改为 View view=inflater.inflate(R.layout.girllayout,container)试试;

另外,girllayout的布局内容是怎样的?