gpt4 book ai didi

android - 如何将默认导航选项卡 View 更改为自定义选项卡 View ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:41:48 24 4
gpt4 key购买 nike

根据我的代码,我的默认导航选项卡如下所示,

default navigation tab

我的问题是:如何将上面的导航选项卡更改为下面的附加选项卡?

custom navigation tab

我将如何实现这一目标?

我的最低 SDK 版本是 8。

我尝试了很多,但都没有成功。谁能帮帮我?

谢谢。

最佳答案

github下载以下两个类

SlidingTabLayout.java

SlidingTabStrip.java

然后创建你的xml文件

<com.emaple.ui.SlidingTabLayout
android:id="@+id/sliding_tabs_home"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<android.support.v4.view.ViewPager
android:id="@+id/viewpager_home"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="@android:color/white" />

并在 Activity 或 fragment 中像这样 intiliaze

 private void init() {
viewpagerHome.setAdapter(new SamplePagerAdapter());
slidingTabsHome.setViewPager(viewpagerHome);
}

SamplePagerAdapter.java

class SamplePagerAdapter extends PagerAdapter {

/**
* @return the number of pages to display
*/
@Override
public int getCount() {
return 10;
}

/**
* @return true if the value returned from {@link #instantiateItem(ViewGroup, int)} is the
* same object as the {@link View} added to the {@link ViewPager}.
*/
@Override
public boolean isViewFromObject(View view, Object o) {
return o == view;
}

/**
* Return the title of the item at {@code position}. This is important as what this method
* returns is what is displayed in the {@link SlidingTabLayout}.
* <p/>
* Here we construct one using the position value, but for real application the title should
* refer to the item's contents.
*/
@Override
public CharSequence getPageTitle(int position) {
return "Item " + (position + 1);
}

/**
* Instantiate the {@link View} which should be displayed at {@code position}. Here we
* inflate a layout from the apps resources and then change the text view to signify the position.
*/
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Inflate a new layout from our resources
View view = HomeActivity.this.getLayoutInflater().inflate(R.layout.pager_item,
container, false);
// Add the newly created View to the ViewPager
container.addView(view);

// Retrieve a TextView from the inflated View, and update it's text
TextView title = (TextView) view.findViewById(R.id.item_title);
title.setText(String.valueOf(position + 1));

Log.i(LOG_TAG, "instantiateItem() [position: " + position + "]");

// Return the View
return view;
}

/**
* Destroy the item from the {@link ViewPager}. In our case this is simply removing the
* {@link View}.
*/
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
Log.i(LOG_TAG, "destroyItem() [position: " + position + "]");
}

}

关于android - 如何将默认导航选项卡 View 更改为自定义选项卡 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30272226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com