TableLayout与ViewPager的简单应用

本文介绍了如何将TableLayout与ViewPager简单结合,使用Fragment,并展示了相关属性设置,如颜色、布局模式和背景颜色。示例代码包括Fragment布局、适配器及Activity。

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

简单实现了TableLay与ViewPager的简单结合,这里用的是fragment;
效果图如下:
这里写图片描述
这里写图片描述
首先,要使用TabLayout必须在AndroidStudio中导入这个依赖库(根据自己编译sdk版本选择)

compile 'com.android.support:design:25.1.0'

主界面布局:注意这里的tablelayout要用android.support.design.widget.TabLayout中的而不是android.widget.TableLayout;另外注意app的命名空间

<?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:orientation="vertical">
    <android.support.design.widget.TabLayout
        android:id="@+id/main_tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabSelectedTextColor="@android:color/white"
        app:tabTextColor="#000000"
        app:tabIndicatorColor="#FF4081"
        app:tabGravity="fill"
        app:tabBackground="@android:color/holo_blue_bright">
    </android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/main_viewpager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
</LinearLayout>

其中TabLayout有几个属性:

app:tabSelectedTextColor:Tab被选中时字体的颜色
app:tabTextColor :默认Tab字体的颜色
app:tabIndicatorColor:被选中时下划线的颜色
app:tabGravity=”fill” 或者 “center” Tab的布局模式 分别是平铺和居中
app:tabBackground:TabLayout背景的颜色

Fragment的布局

就中间显示一个TextView
注意:fitsSystemWindows=”true”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_ll"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:gravity="center">
    <TextView
        android:id="@+id/fragment_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="@android:color/black"
        android:text="Tab1"/>
</LinearLayout>

Fragment代码


public class ViewPagerFragment extends Fragment {

    private int mTitle;
    private int mColor;
    private TextView mTextView;
    private LinearLayout mLinear;

    public static ViewPagerFragment newInstance(int title , int color){
        ViewPagerFragment fragment = new ViewPagerFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("title",title);
        bundle.putInt("color",color);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTitle = getArguments().getInt("title");
        mColor = getArguments().getInt("color");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment, container, false);
        mTextView = (TextView) view.findViewById(R.id.fragment_textView);
        mTextView.setText("Page"+(mTitle + 1));
        mLinear = (LinearLayout) view.findViewById(R.id.fragment_ll);
 /**这里注意是setBackgroundResource不是setBackgroundColor;setBackgroundResource(int resId)方法的参数是一个组件的id值。该方法也是用于加载组件的背景图片的;setBackgroundColor(Color.XXX)方法参数为一个Color类的静态常量.顾名思义,它是用来设置背景颜色的方法.*/
        mLinear.setBackgroundResource(mColor);
        return view;
    }
}

Adapter

public class MyAdapter extends FragmentPagerAdapter {
    private int mCount = 3 ;
    private int[] mColors = new int[]{android.R.color.holo_orange_dark,android.R.color.holo_green_dark
            ,android.R.color.holo_blue_dark};
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return ViewPagerFragment.newInstance(position,mColors[position]);
    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "Page" + (position + 1);
    }
}

Activity

public class MainActivity extends AppCompatActivity {


    private MyAdapter mAdapter;
    private ViewPager mViewPager;
    private TabLayout mTableLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAdapter = new MyAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.main_viewpager);
        mViewPager.setAdapter(mAdapter);
        mTableLayout = (TabLayout) findViewById(R.id.main_tab);
        mTableLayout.setupWithViewPager(mViewPager);
        mTableLayout.setTabMode(TabLayout.MODE_FIXED);

    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值