如何在不更改actionBar的情况下自定义android选项卡? (使用ViewPager)

时间:2015-09-01 20:58:23

标签: android android-layout android-actionbar android-tabs

我不确定这是否可行。我正在尝试在每个选项卡中使用选项卡和列表进行屏幕显示(我已经完成了)并且我已经为标题和一些图标做了自定义ActionBar,现在,我想要更改文本颜色选项卡,我已经解决了背景颜色但我找不到任何与文本颜色相关的内容,这里是我的代码.java和我的manifest.xml

addMenu();

        // Set up the action bar.
        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffff")));
        actionBar.show();

        actionBar.setDisplayOptions(actionBar.getDisplayOptions() | ActionBar.DISPLAY_SHOW_CUSTOM);


        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());


        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            // Create a tab with text corresponding to the page title defined by
            // the adapter. Also specify this Activity object, which implements
            // the TabListener interface, as the callback (listener) for when
            // this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setText(mSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this)
                            );

        }

addMenu方法(仅更改操作栏):

getSupportActionBar().setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM);
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.order_title, null);
    TextView textView = (TextView) view.findViewById(R.id.mytext);
    textView.setWidth(getWindow().getWindowManager().getDefaultDisplay().getWidth());
    textView.setPadding(0, 0, 50, 0);
    textView.setGravity(Gravity.CENTER);

    Typeface segoeui = Typeface.createFromAsset(getAssets(),"fonts/segoeui.ttf");
    textView.setTypeface(segoeui);
    textView.setText("Some Text");
    getSupportActionBar().setCustomView(view);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.arrow_left);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#afafaf")));

和我的Manifest.xml:

<activity
        android:name=".Activity1"
        android:label="@string/title_activity1"
        android:parentActivityName=".MainActivity"
        android:screenOrientation="portrait"
        >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="cleanpress.cleanpress.MainActivity" />
    </activity>

它的外观如下: http://i.stack.imgur.com/0hRbv.png

正如您所看到的,我正在工作的屏幕与另一个屏幕相关(如果相关的话)。 我想改变标签的文字颜色(有3个标签),有人可以帮我吗?

(我已经使用HTML格式但没有工作)

2 个答案:

答案 0 :(得分:1)

You should use android.support.design.widget.TabLayout to handle Tabs so you can adjust:

    app:tabIndicatorColor="@color/colorAccent" //indicator color
    app:tabSelectedTextColor="#EEE" // text color on selected tab
    app:tabTextColor="#EEE" // text color on tab

you should also use android.support.v7.widget.Toolbar for ActionBar.

Exmaple:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

            <android.support.v7.widget.Toolbar
                android:layout_alignParentTop="true"
                android:text="@string/app_name"
                android:id="@+id/app_bar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

            <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_below="@+id/app_bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                app:tabIndicatorColor="@color/colorAccent"
                app:tabSelectedTextColor="#EEE"
                app:tabTextColor="#EEE" />


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

</RelativeLayout>

In activity in onCreate

        Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        mAdapter = new YourPagerAdapter(getSupportFragmentManager());
        mPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mPager);

Remember to add

compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:support-v4:23.0.0'

in gradle dependencies

答案 1 :(得分:0)

在样式中尝试这个

     <style name="MyTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
 <item name="android:actionBarTabTextStyle">@style/Widget.MyTabText</item>
 </style>

 <style name="Widget.MyTabText" parent="@style/Widget.AppCompat.ActionBar.TabText">
        <item name="android:textColor">@color/#your_color</item>
 </style>