gpt4 book ai didi

android - 使用 Material Design 在工具栏中滑动标签

转载 作者:可可西里 更新时间:2023-11-01 19:06:46 25 4
gpt4 key购买 nike

我一直在使用 this post 学习使用 Material Design 使用滑动标签.我已经设法在 Toolbar下方 实现了 SlidingTabs,就像这样:

enter image description here

但现在我想创建ActionBar/ToolBar Fragment Tabs ...

最佳答案

我能够准确地重新创建您想要实现的内容。我正在使用这个 Library对于选项卡。

这是我创建的 View :

enter image description here

通过依赖导入库或下载项目并手动导入

compile 'com.jpardogo.materialtabstrip:library:1.0.9'

styles.xml

<resources>

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
</style>
</resources>

MainActivity 和适配器

public class MainActivity extends ActionBarActivity {
Toolbar toolbar;
ViewPager viewPager;
ContactPagerAdapter pagerAdapter;
PagerSlidingTabStrip pagerSlidingTabStrip;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setLogo(R.mipmap.logo_two);
toolbar.inflateMenu(R.menu.menu_main);
viewPager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new ContactPagerAdapter(this, getSupportFragmentManager());
pagerSlidingTabStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
viewPager.setAdapter(pagerAdapter);
pagerSlidingTabStrip.setViewPager(viewPager);
}

public static class ContactPagerAdapter extends FragmentPagerAdapter implements PagerSlidingTabStrip.CustomTabProvider {

private final int[] ICONS = {R.mipmap.ic_launcher, R.mipmap.ic_launcher};
Context mContext;
private Fragment f = null;

public ContactPagerAdapter(Context ctx, FragmentManager fm) {
super(fm);
mContext = ctx;
}

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

@Override
public Fragment getItem(int position) { // Returns Fragment based on position
switch (position) {
case 0:
f = new FragmentPageOne();
break;
case 1:
f = new FragmentPageTwo();
break;
}
return f;
}

@Override
public View getCustomTabView(ViewGroup parent, int position) {
LinearLayout customLayout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.custom_tab, parent, false);
ImageView imageView = (ImageView) customLayout.findViewById(R.id.image);
imageView.setImageResource(ICONS[position]);
return customLayout;
}
}
}

activity_main.xml

<RelativeLayout
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.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#02a6d8"
android:minHeight="56dp"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="wrap_content"
android:layout_height="56dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="8dp"
android:background="#02a6d8"
app:pstsDividerColor="#02a6d8"
app:pstsIndicatorColor="#fff"
app:pstsIndicatorHeight="2dp"
app:pstsShouldExpand="false"
app:pstsUnderlineHeight="0dp"/>
</android.support.v7.widget.Toolbar>


<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/toolbar"/>

<!-- Shadow below toolbar-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_below="@+id/toolbar"
android:background="@drawable/toolbar_shadow"/>
</RelativeLayout>

custom_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="30dp"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/image"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="8dp"
android:paddingTop="8dp"/>
</LinearLayout>

可绘制工具栏_shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="270"
android:endColor="@android:color/transparent"
android:startColor="@color/semi_transparent"/>
</shape>

关于android - 使用 Material Design 在工具栏中滑动标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30184985/

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