Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何自定义单个选项卡? (改变背景颜色,指示颜色和文字颜色)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在此链接How do I apply a style programmatically? @H_696_2@凯文格兰特给了这个问题的解释,我的问题是他的代码是上下文部分.准确一点 :

ctv = new CustomView(context,R.attr.tabStyleAttr);
@H_696_2@在这代码中,它表示:上下文无法解析为变量

@H_696_2@我想将特定的风格应用于标签,这就是为什么设置主题对我来说不起作用.当然也欢迎任何我的问题的替代品.

@H_696_2@我尝试更改操作栏选项卡的背景颜色,指示器颜色和文本颜色.

@Override
public void onTabSELEcted(ActionBar.Tab tab,Fragmenttransaction fragmenttransaction) 
{
    CustomView ctv;
    ctv = new CustomView(this,R.attr.tabStyleAttr);        
    tab.setCustomView(ctv);  
    mViewPager.setCurrentItem(tab.getPosition());
}
@H_696_2@styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.Ab" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/abStyle</item> 
        <item name="@attr/actionBarTabStyle">@style/tabStyle</item>        
        <item name="android:actionBarTabTextStyle">@style/tabTextColor</item>
    </style>   

    <style name="abStyle" parent="@android:style/Widget.Holo.Light.ActionBar.solid">
        <item name="android:BACkground">@drawable/ab_solid_style</item>
        <item name="android:BACkgroundStacked">@drawable/ab_stacked_solid_style</item>
        <item name="android:BACkgroundSplit">@drawable/ab_bottom_solid_style</item>
        <item name="android:height">100dp</item>
    </style>    

    <style name="tabStyle" parent="@android:style/Widget.Holo.Light.ActionBar.TabView">

        <item name="android:BACkground">@drawable/tab_inDicator_ab_style</item>
    </style>

    <style name="tabTextColor" parent="@android:style/Widget.Holo.Light.ActionBar.TabText">
        <item name="android:textColor">@android:color/white</item>
    </style>



</resources>
@H_696_2@mainActivity.java(oncreate)

public void onCreate(Bundle savedInstanceStatE)
    {       
        super.onCreate(savedInstanceStatE);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the three priMary sections
        // of the app.
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        //set custom actionbar
        actionBar.setCustomView(R.layout.titlebar);
        //Displays the custom design in the actionbar
        actionBar.setDisplayShowCustomEnabled(true);
        //Turns the homeIcon a View     
        View homeIcon = findViewById(android.R.id.homE);
        //Hides the View (and so the icon)
        ((View)homeIcon.getParent()).setVisibility(View.GONE);

        // Specify that we will be displaying tabs in the action bar.
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set up the ViewPager,attaching the adapter and setTing up a listener for when the
        // user swipes between sections.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mAppSectionsPagerAdapter);

        mViewPager.setOnPagechangelistener(new ViewPager.SimpLeonPagechangelistener()
        {           
            @Override
            public void onPageSELEcted(int position)
            {
                    // When swiping between different app sections,SELEct the corresponding tab.
                    // We can also use ActionBar.Tab#SELEct() to do this if we have a reference to the Tab.
                    actionBar.setSELEctedNavigationItem(position);
            }
        });

        // For each of the sections in the app,add a tab to the action bar.
        for (int i = 0; i < mAppSectionsPagerAdapter.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
            // listener for when this tab is SELEcted.
            Tab tab = actionBar.newTab().setText(mAppSectionsPagerAdapter.getPagetitle(i)).setTabListener(this);            
            actionBar.addTab(tab);
        }
    }
@H_696_2@这是我想做的:

@H_696_2@对于使用Views的新结果发生了

@H_696_2@mainActivity.java

package com.example.android.effectivenavigation;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Fragmenttransaction;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
    AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    //The viewpager displays on of the section at a time
    ViewPager mViewPager;

    public void onCreate(Bundle savedInstanceStatE)
    {       
        super.onCreate(savedInstanceStatE);
        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the three priMary sections
        // of the app.
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        //set custom actionbar
        actionBar.setCustomView(R.layout.titlebar);
        //Displays the custom design in the actionbar
        actionBar.setDisplayShowCustomEnabled(true);
        //Turns the homeIcon a View     
        View homeIcon = findViewById(android.R.id.homE);
        //Hides the View (and so the icon)
        ((View)homeIcon.getParent()).setVisibility(View.GONE);


        // Specify that we will be displaying tabs in the action bar.
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Set up the ViewPager,SELEct the corresponding tab.
                    // We can also use ActionBar.Tab#SELEct() to do this if we have a reference to the Tab.
                    actionBar.setSELEctedNavigationItem(position);
            }
        });
       /*final Tab firstTab = actionBar.newTab()
                .setText(mAppSectionsPagerAdapter.getPagetitle(0))
                .setTabListener(this)
                .setCustomView(R.id.nieuws_tab_layout);
        /*final Tab secondTab = actionBar.newTab()
                 .setText(mAppSectionsPagerAdapter.getPagetitle(1))
                 .setCustomView(R.id.nieuws_tab_layout);
        final Tab thirdTab = actionBar.newTab()
                .setText(mAppSectionsPagerAdapter.getPagetitle(2))
                .setCustomView(R.id.nieuws_tab_layout);

        actionBar.addTab(firstTab);
        actionBar.addTab(secondTab);
        actionBar.addTab(thirdTab);*/

        // For each of the sections in the app,add a tab to the action bar.
        for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++)
        {
            if(i == 0)
            {
                final View firstCustomView = new CustomView(this);
                //firstCustomView.setBACkgroundColor(Color.bLUE);
                Tab tab = actionBar.newTab().setText(mAppSectionsPagerAdapter.getPagetitle(i)).setTabListener(this).setCustomView(R.layout.nieuws_tab_layout);
                actionBar.addTab(tab);
            }
            else
            {
            // Create a tab with text corresponding to the page title defined by the adapter.
            // Also specify this Activity object,as the
            // listener for when this tab is SELEcted.
            Tab tab = actionBar.newTab().setText(mAppSectionsPagerAdapter.getPagetitle(i)).setTabListener(this);            
            actionBar.addTab(tab);
            }
        }
    }

    @Override
    public void onTabUnSELEcted(ActionBar.Tab tab,Fragmenttransaction fragmenttransaction) 
    {
    }

    @Override
    public void onTabSELEcted(ActionBar.Tab tab,Fragmenttransaction fragmenttransaction) 
    {
        //CustomView ctv;
        //ctv = new CustomView(context,R.attr.tabStyleAttr);
        // When the given tab is SELEcted,switch to the corresponding page in the ViewPager.
        //LayoutInflater inflater = (LayoutInflater) getSystemservice(LAYOUT_INFLATER_serviCE);
        //View tabView = inflater.inflate(R.layout.nieuws_tab_layout,null);
        //tabView.setBACkgroundColor(0xFF00FF00);
        //tab.setCustomView(tabView);  
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReSELEcted(ActionBar.Tab tab,Fragmenttransaction fragmenttransaction)
    {
    }

    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter 
    {
        public AppSectionsPagerAdapter(FragmentManager fm)
        {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) 
        {
            switch (i)
            {
                case 0:
                    // The first section of the app is the most interesTing -- it offers
                    // a launchpad into the other demonstrations in this example application.
                    return new LaunchpadSectionFragment();

                default:
                    // The other sections of the app are dummy placeholders.
                    Fragment fragment = new DummySectionFragment();
                    Bundle args = new Bundle();
                    args.puTint(DummySectionFragment.ARG_SECTION_numbER,i + 1);
                    fragment.setArguments(args);
                    return fragment;
            }
        }

        @Override
        public int getCount()
        {
            return 3;
        }

        @Override
        public CharSequence getPagetitle(int position) 
        {
            switch(position)
            {
                case 0:
                {
                    return "Tab1";
                }
                case 1:
                {
                    return "Tab2";
                }
                case 2:
                {
                    return "Tab3";
                }
                default:
                {
                    return "Section " + (position + 1);
                }
            }
        }
    }
    public static class LaunchpadSectionFragment extends Fragment
    {
        @Override
        public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceStatE)
        {
            View rootView = inflater.inflate(R.layout.fragment_section_launchpad,container,falsE);

            // Demonstration of a collection-browsing activity.
            rootView.findViewById(R.id.demo_collection_button).setOnClickListener(new View.onClickListener()
            {
                @Override
                public void onClick(View view) 
                {
                    Intent intent = new Intent(getActivity(),CollectionDemoActivity.class);
                    startActivity(intent);
                }
            });

            // Demonstration of navigaTing to external activities.
            rootView.findViewById(R.id.demo_external_activity).setOnClickListener(new View.onClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    // Create an intent that asks the user to pick a photo,but using
                    // FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET,ensures that relaunching
                    // the application from the device home screen does not return
                    // to the external activity.
                    Intent externalActivityIntent = new Intent(Intent.ACTION_PICK);
                    externalActivityIntent.setType("image/*");
                    externalActivityIntent.addFlags(
                    Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                    startActivity(externalActivityIntent);
                }
            });
            return rootView;
        }
    }

    /**
     * A dummy fragment represenTing a section of the app,but that simply displays dummy text.
     */
    public static class DummySectionFragment extends Fragment
    {
        public static final String ARG_SECTION_numbER = "section_number";

        @Override
        public View onCreateView(LayoutInflater inflater,Bundle savedInstanceStatE)
        {
            View rootView = inflater.inflate(R.layout.fragment_section_dummy,falsE);
            Bundle args = getArguments();
            ((TextView) rootView.findViewById(android.R.id.text1)).setText(getString(R.String.dummy_section_text,args.geTint(ARG_SECTION_numbER)));
            return rootView;
        }
    }
    public class CustomView extends View
    {
        public CustomView(Context context)
        {
            super(context,null);
        }
    } 
}
@H_696_2@tab_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://scheR_347_11845@as.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

  <TextView
              android:id="@+id/nieuws_tab_layout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"              
              android:text="@String/nieuws"
              android:gravity="center_vertical"
              android:layout_marginTop="15dp"
              android:textColor="@android:color/white"
              android:textStyle="bold"
              android:BACkground="@android:color/black"
              />
</LinearLayout>

解决方法

只需在标签创建时间设置自定义视图,就像:
final Tab firstTab = actionBar.newTab()
                              .setText(mAppSectionsPagerAdapter.getPagetitle(0))
                              .setCustomView(R.id.custom_tab_view_red);
final Tab secondTab = actionBar.newTab()
                               .setText(mAppSectionsPagerAdapter.getPagetitle(1))
                               .setCustomView(R.id.custom_tab_view_bluE);
// etc

actionBar.addTab(firstTab);
actionBar.addTab(secondTab);
// etc
@H_696_2@inCreate().

@H_696_2@您还必须在xml布局文件(而不是样式)中定义与上述ids对应的视图.

@H_696_2@或者,如果要直接创建视图:

final View firstCustomView = new CustomView(this);
firstCustomView.setBACkgroundColor(Color.bLUE);  // or with drawable or resource
final Tab firstTab = actionBar.newTab()
                              .setText(mAppSectionsPagerAdapter.getPagetitle(0))
                              .setCustomView(firstCustomView);
actionBar.addTab(firstTab);
// then same for other tabs,just with another color
@H_696_2@留下以下资料供参

@H_696_2@要定义一个这样的视图,您需要指定一个Android上下文.这通常是将显示选项卡的活动.
假设您在Activity中初始化您的选项卡,只需将Activity实例作为上下文传递:

ctv = new CustomView(this,R.attr.tabStyleAttr);
@H_696_2@如果从里面的活动,或者例如:

ctv = new CustomView(getActivity(),R.attr.tabStyleAttr);
@H_696_2@如果从片段等

@H_696_2@至于设置动作栏选项卡的特定样式,则无需像程序中那样以编程方式创建自定义视图.先读一点about the action bar,然后查看他们提供的the example.您可以看到,您可以在xml中指定选项卡样式:

@H_696_2@例如.

@H_696_2@有关完全匹配您的用例的完整示例,请参阅此Android文档:https://developer.android.com/training/basics/actionbar/styling.html#CustomTabs.注意使用状态列表来实现“选择时的样式”.

大佬总结

以上是大佬教程为你收集整理的android – 如何自定义单个选项卡? (改变背景颜色,指示颜色和文字颜色)全部内容,希望文章能够帮你解决android – 如何自定义单个选项卡? (改变背景颜色,指示颜色和文字颜色)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。