【问题标题】:Is it Possible to apply styles for tab titles是否可以为选项卡标题应用样式
【发布时间】:2016-08-24 11:01:32
【问题描述】:

在我的选项卡布局中,我有 3 个选项卡。此选项卡的标题默认全部大写 我正在使用标签布局并以 NoActionBar 作为样式查看寻呼机。

【问题讨论】:

  • 是的..它可能
  • 你能发布答案吗?我们如何应用这些样式?
  • 当然..给我几分钟..

标签: android tabs styles


【解决方案1】:

无法将样式应用于默认选项卡标题。以下链接将帮助您创建自定义选项卡。

http://mrbool.com/how-to-customize-tab-layout-in-android/28153

【讨论】:

  • 该链接对于创建自定义标签非常有用。
【解决方案2】:

是的,你可以,只要按照这个

custom_tab_item:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tab"
    android:gravity="center"
    android:textColor="@android:color/white"
android:textSize="@dimen/tab_label"/>

main_layout:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_scrollFlags="scroll|enterAlways">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textStyle="bold"
            style="@android:style/TextAppearance.Medium"
            android:text="Toolbar Title" />


    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:tabGravity="fill"
        app:tabIndicatorColor="@android:color/white"
        app:tabMode="fixed" />

Activity:

    public class DiningActivity extends AppCompatActivity {

    private Toolbar mToolbar;

    private TabLayout mTabLayout;

    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_dining);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        ((TextView) findViewById(R.id.toolbar_title)).setText(R.string.dining);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(mViewPager);

        mTabLayout = (TabLayout) findViewById(R.id.tabs);
        mTabLayout.setupWithViewPager(mViewPager);
        setupTabs();
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new RestaurantFragment(), getString(R.string.restaurants));
        adapter.addFragment(new HotelsFragment(), getString(R.string.hotels));
        adapter.addFragment(new BarsPubsFragment(), getString(R.string.bars_pubs));
        viewPager.setAdapter(adapter);
    }

    private void setupTabs() {

        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab_item, null);
        tabOne.setText(getString(R.string.restaurants));
        mTabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab_item, null);
        tabTwo.setText(getString(R.string.hotels));
        mTabLayout.getTabAt(1).setCustomView(tabTwo);

        TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab_item, null);
        tabThree.setText(getString(R.string.bars_pubs));
        mTabLayout.getTabAt(Numerics.TWO).setCustomView(tabThree);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            super.onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
}

【讨论】:

  • 如果我们想要应用任何尺寸和样式,我们可以在 custom_tab_item.layout 中提及。我知道这是对还是错?
  • 我尝试使用您的代码创建示例,但我收到 tabsetup 的空点错误。
  • 如果不使用自定义选项卡,是否无法为默认选项卡应用样式。
【解决方案3】:

在android.support.design.widget.TabLayout里面这样设置主题属性

   android:theme="@style/Base.Widget.Design.TabLayout"

并在样式中定义主题

<style name="Base.Widget.Design.TabLayout" parent="android:Widget">
    <item name="android:fontFamily">sans-serif</item>
    <item name="android:textStyle">normal</item>
</style>

【讨论】:

  • 它不是工作样式应用于选项卡中的文本而不是选项卡标题。我想将样式应用于 选项卡标题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多