首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

android:如何从Android Support Lib版本4向PagerTabStrip添加图标/ drawable?

在Android Support Lib版本4中,PagerTabStrip并不直接支持添加图标或drawable。但是,您可以通过自定义PagerAdapter和PagerTabStrip来实现这个功能。以下是一个简单的示例:

  1. 首先,创建一个自定义PagerAdapter,继承自android.support.v4.view.PagerAdapter:
代码语言:java
复制
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomPagerAdapter extends PagerAdapter {
    private Context mContext;
    private Drawable[] mDrawableArray;
    private String[] mTitles;

    public CustomPagerAdapter(Context context, Drawable[] drawableArray, String[] titles) {
        mContext = context;
        mDrawableArray = drawableArray;
        mTitles = titles;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.pager_item, container, false);

        ImageView imageView = view.findViewById(R.id.imageView);
        TextView textView = view.findViewById(R.id.textView);

        imageView.setImageDrawable(mDrawableArray[position]);
        textView.setText(mTitles[position]);

        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public int getCount() {
        return mDrawableArray.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
}
  1. 创建一个新的布局文件(例如:pager_item.xml),用于显示图标和标题:
代码语言:xml
复制
<?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"
    android:gravity="center">

   <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"/>

   <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="@android:color/white"/>

</LinearLayout>
  1. 在您的主布局文件中添加PagerTabStrip和ViewPager:
代码语言:xml<android.support.v4.view.ViewPager
复制
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <android.support.v4.view.PagerTabStrip
        android:id="@+id/pagerTabStrip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:paddingTop="4dp"
        android:paddingBottom="4dp"
        android:textColor="@android:color/white"
        android:background="@android:color/darker_gray"/>

</android.support.v4.view.ViewPager>
  1. 在您的Activity或Fragment中初始化ViewPager和PagerTabStrip,并设置自定义PagerAdapter:
代码语言:java
复制
public class MainActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private PagerTabStrip mPagerTabStrip;

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

        mViewPager = findViewById(R.id.viewPager);
        mPagerTabStrip = findViewById(R.id.pagerTabStrip);

        // 设置ViewPager的滚动监听器,以便在滚动时更新PagerTabStrip的显示
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                mPagerTabStrip.setTabIndicatorPosition(position + positionOffset);
            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });

        // 创建自定义PagerAdapter
        Drawable[] drawableArray = new Drawable[]{
                ContextCompat.getDrawable(this, R.drawable.ic_home),
                ContextCompat.getDrawable(this, R.drawable.ic_search),
                ContextCompat.getDrawable(this, R.drawable.ic_settings)
        };
        String[] titles = new String[]{"Home", "Search", "Settings"};
        CustomPagerAdapter adapter = new CustomPagerAdapter(this, drawableArray, titles);

        // 设置PagerAdapter
        mViewPager.setAdapter(adapter);
        mPagerTabStrip.setTabIndicatorColor(ContextCompat.getColor(this, R.color.colorAccent));
    }
}

现在,您应该可以在PagerTabStrip中看到带有图标的标签。请注意,这个示例使用了Android Support Lib的最新版本,您可能需要根据您的项目需求进行调整。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

android项目的目录结构

/**************2016年4月23更新*********************/ 相关技术: 知乎:用eclipse做Android开发,新建工程时应如何选择Android版本?...赠送Support Library Version: 新版本的sdk中会有一些新的方法,但是旧的Android版本中并不存在这些方法。...为了能让旧版本Android中也能使用一些新的特性,就可以使用一个额外的lib放到程序里面,这样旧版本也能这种办法来实现新特性了。例如,在2.3上使用ActionBar,Fragment等。...libs : 第三方库,例如:v4包 因为4.0以上添加了很多新的api在2.0上没有所以要用这个jar包 res : 资源目录 Drawable-hdpi drawable图片资源 h hign 高分辨率...-- [icon图标,应用名称] --> <application android:icon="@drawable/ic_launcher" android:label

88620

android 中 vector 的用法 ,坑 ,怎么替代,关于这几方面的一些看法

在安卓的发展历程中,由于设备碎片化的原故,谷歌在app中图标的适配上做出一步又一步的改进,大体有这么几个阶段: 首先有了drawable-(m|h|xh|xxh|xxxh)dpi 自android studio...,AnimatedVectorDrawableCompat As you may have seen on the Support Lib 23.2.0 blog post, we now have...用TextView的setText设置图标, setTextSize设置大小, 用TextColor设置图标颜色 ,只要能显示String的控件,都可以用,这样说来如何 ? <android.support.v7.widget.AppCompatTextView android...嗯,这个并不算是什么高科技, 只是一个字体而已, 我就不长篇大论了, 下面贴出获取的流程吧,图官网拿的: 选中一堆需要的图标并加入购物车, 然后再这里点下载到本地, 其中iconfont.ttf

66230
领券