首页 技术 正文
技术 2022年11月15日
0 收藏 820 点赞 4,820 浏览 4722 个字
 <android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
1  TabLayout tabs = (TabLayout) this.findViewById(R.id.tabs);
tabs.addTab(tabs.newTab().setText("Tab1"));
tabs.addTab(tabs.newTab().setText("Tab2"));
tabs.addTab(tabs.newTab().setText("Tab3"));
tabs.addTab(tabs.newTab().setText("Tab4"));
tabs.addTab(tabs.newTab().setText("Tab5"));

61、常规控件(4)TabLayout-便捷实现标签   

注:如果Tab标签特别多的话,控件是支持自动滑动的,只需要在控件加入 app:tabMode=”scrollable”  这个属性即可。

以上简单两步,就可以实现如图效果了。但是这个远远不能满足我们的需要,所以接着往下看。

给以上控件添加不同的颜色。如下图效果:

61、常规控件(4)TabLayout-便捷实现标签

 <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">
<!--
app:tabTextColor 未选中字体颜色
app:tabSelectedTextColor 选中的字体颜色
app:tabIndicatorColor 下标指示器的颜色
app:tabIndicatorHeight 下标指示器的高度
-->

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_dark"
app:tabTextColor="@android:color/black"
app:tabSelectedTextColor="@android:color/holo_red_light"
app:tabIndicatorColor="@android:color/holo_orange_light"
app:tabIndicatorHeight="5dp"

/>
</LinearLayout>

更多的使用是与ViewPager相结合来使用。

效果:Title可以左右滑动切换;同时也可以利用ViewPager左右滑动切换。

61、常规控件(4)TabLayout-便捷实现标签61、常规控件(4)TabLayout-便捷实现标签

代码步骤如下:

1、MainActivity的布局文件,activity_main.xml。

 <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">
<!--
app:tabTextColor 未选中字体颜色
app:tabSelectedTextColor 选中的字体颜色
app:tabIndicatorColor 下标指示器的颜色
app:tabIndicatorHeight 下标指示器的高度
app:tabMode="scrollable" 横向滚动
-->
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_green_dark"
app:tabTextColor="@android:color/black"
app:tabSelectedTextColor="@android:color/holo_red_light"
app:tabIndicatorColor="@android:color/holo_orange_light"
app:tabIndicatorHeight="5dp"
app:tabMode="scrollable"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

2、MainActivity.java

 package com.example.com.designdemo; import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tabs = (TabLayout) this.findViewById(R.id.tabs); List<String> titles = new ArrayList<String>();
List<Fragment> fragments = new ArrayList<Fragment>();
for (int i=0; i<10; i++) {
String title = "Tab"+ (i+1);
tabs.addTab(tabs.newTab().setText(title));
titles.add(title); Fragment fragment = new TestOneFragment(title);
fragments.add(fragment);
} ViewPager viewpager = (ViewPager) this.findViewById(R.id.viewpager);
TestOneAdapter mAdapter = new TestOneAdapter(getSupportFragmentManager(), titles, fragments);
viewpager.setAdapter(mAdapter);
tabs.setupWithViewPager(viewpager);
tabs.setTabsFromPagerAdapter(mAdapter);
} }

3、新建一个TestOneFragment.java,添加ViewPager使用。

 package com.example.com.designdemo; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class TestOneFragment extends Fragment {
public String mTitle = null; public TestOneFragment(String title) {
this.mTitle = title;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.test_one_fragment, container, false); TextView txt = (TextView) view.findViewById(R.id.txt);
txt.setText(mTitle);

return view;
}
}

4、Fragment适配器TestOneAdapter.java

 package com.example.com.designdemo; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import java.util.List; public class TestOneAdapter extends FragmentStatePagerAdapter { List<String> titles;
List<Fragment> fragments; public TestOneAdapter(FragmentManager fm, List<String> titles, List<Fragment> fragments) {
super(fm);
this.titles = titles;
this.fragments = fragments;
} @Override
public Fragment getItem(int position) {
return fragments.get(position);
} @Override
public int getCount() {
return fragments.size();
} @Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}

 完整DEMO下载地址:http://download.csdn.net/detail/androidsj/9301597

下一篇: Thrift初试
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,501
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,915
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,748
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,505
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,143
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,306