如何将Tabmenu文本更改为较小的案例?

时间:2016-01-18 06:34:14

标签: java android

我知道默认情况下tabmenu文本以大写字母显示但我想更改像这个图像的较小字母中的tabmenu文本

enter image description here

Style.xml

<resources>

<style name="AppTheme" parent="AppTheme.Base">

</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColor">@color/textColorPrimary</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="android:textColorSecondary">@color/textColorPrimary</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="TabTexTheme" parent="TabTexTheme.Base">

</style>

<style name="TabTexTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:textStyle">bold</item>
    <item name="android:actionBarTabTextStyle">@style/TabtextStyle</item>
    <item name="actionBarTabTextStyle">@style/TabtextStyle</item>

</style>

<style name="TabtextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu">
    <item name="android:textAllCaps">false</item>
    <item name="android:background">@color/colorTabBackground</item>
    <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

MainActivity.java

    public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_information_category1);
        Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar_information);
        setSupportActionBar(toolbar);

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

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new TodayInformationFragment(), "Today");
        adapter.addFragment(new ThisWeekInformationFragment(), "This Week");
        adapter.addFragment(new ThisMonthInformationFragment(), "This Month");
        adapter.addFragment(new AllInformationFragment(), "All");
        viewPager.setAdapter(adapter);
    }
    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}

activitymain.xml

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

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

    <RelativeLayout
        android:id="@+id/layout_information"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.vimalsagarji.vimalsagarjiapp.InformationCategory1">

        <include
            android:id="@+id/toolbar_information"
            layout="@layout/toolbar_home"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <EditText
            android:id="@+id/etText"
            android:layout_below="@+id/toolbar_information"
            android:hint="    Information cat 1"
            android:textColorHint="@color/colorPrimary"
            android:background="@color/colorTabBackground"
            android:layout_width="match_parent"
            android:focusable="false"
            android:drawableRight="@drawable/search"
            android:layout_height="50dp"/>

        <View
            android:id="@+id/line"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginTop="-7dp"
            android:layout_below="@+id/etText"
            android:background="@color/colorPrimary"/>
    </RelativeLayout>
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/TabtextStyle"
        app:tabTextColor="@color/colorTabColor"
        app:tabSelectedTextColor="@color/colorTabColor"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
    <View
        android:id="@+id/line1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="-5dp"
        android:layout_below="@+id/tabs"
        android:background="@color/colorPrimary"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

我使用了此代码,但tabmenu文本没有变化。它仍显示为大写字母

3 个答案:

答案 0 :(得分:0)

试试这段代码它会对你有帮助。

<android.support.design.widget.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:tabIndicatorColor="@android:color/white"
                    app:tabIndicatorHeight="2dp"
                    app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
                    app:tabSelectedTextColor="@android:color/white"
                    app:tabTextColor="@android:color/white" />

答案 1 :(得分:0)

 TextView textview = (TextView)  mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title);
textview.setTextSize(25);

设置适配器

后设置此项

但是根据屏幕尺寸使用文字大小,如

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

for(int i = 0; i <= 3; i++){
    textview = (TextView)  mTabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
    if(width == 720 || height == 1184){
        textview.setTextSize(10);
    }else if (width > 720 || height > 1184) {
        textview.setTextSize(12);
    }else if (width > 900 || height > 1500) {
        textview.setTextSize(15);
    }
    textview.setTextColor(Color.parseColor("#E73421")); // To set text color
}
for(int i = 0; i <= 3; i++){ 3中的

表示您拥有的标签数(0,1,2,3)

答案 2 :(得分:0)

创建您的客户风格,如下所示

<style name="TextAppearance.Widget.CustomTabWidget">
        <item name="textSize">16sp</item>
        <item name="textStyle">normal</item>
        <item name="textAllCaps">false</item>
        <item name="textColor">@color/tab_indicator_text</item>
    </style>

将此样式应用于tabLayout,如下所示

app:tabTextAppearance="@android:style/TextAppearance.Widget.CustomTabWidget"