在此链接:我如何申请一个样式编程
凯文·格兰特给了一个交代这个问题,我的问题与他的code是上下文的一部分。为了precise:
CTV =新CustomView(背景下,R.attr.tabStyleAttr);
在此code,它说:背景不能被解析为一个变量
我想申请特定的样式选项卡和这就是为什么设置主题不为我工作。任何替代我的问题是值得欢迎的太当然。
我试图改变背景颜色动作条选项卡,指示灯的颜色,文本颜色。
@覆盖
公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction)
{
CustomView CTV;
CTV =新CustomView(这一点,R.attr.tabStyleAttr);
tab.setCustomView(CTV);
mViewPager.setCurrentItem(tab.getPosition());
}
styles.xml
< XML版本=1.0编码=UTF-8&GT?;
<资源>
<样式名称=Theme.Ab父=@安卓风格/ Theme.Holo.Light>
<项目名称=机器人:actionBarStyle> @风格/ abStyle< /项目>
<项目名称=@ ATTR / actionBarTabStyle> @风格/ tabStyle< /项目>
<项目名称=机器人:actionBarTabTextStyle> @风格/ tabTextColor< /项目>
< /风格>
<样式名称=abStyle父=@安卓风格/ Widget.Holo.Light.ActionBar.Solid>
<项目名称=机器人:背景> @可绘制/ ab_solid_style< /项目>
<项目名称=机器人:backgroundStacked> @可绘制/ ab_stacked_solid_style< /项目>
<项目名称=机器人:backgroundSplit> @可绘制/ ab_bottom_solid_style< /项目>
<项目名称=机器人:身高> 100dp< /项目>
< /风格>
<样式名称=tabStyle父=@安卓风格/ Widget.Holo.Light.ActionBar.TabView>
<项目名称=机器人:背景> @可绘制/ tab_indicator_ab_style< /项目>
< /风格>
<样式名称=tabTextColor父=@安卓风格/ Widget.Holo.Light.ActionBar.TabText>
<项目名称=机器人:文字颜色> @android:彩色/白< /项目>
< /风格>
< /资源>
MainActivity.java(的onCreate)
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
//创建将返回一个片段为三个主要部分的适配器
//应用程序的。
mAppSectionsPagerAdapter =新AppSectionsPagerAdapter(getSupportFragmentManager());
//设置操作栏。
最后的动作条动作条= getActionBar();
//设置自定义动作条
actionBar.setCustomView(R.layout.titlebar);
//显示定制设计的动作条
actionBar.setDisplayShowCustomEnabled(真正的);
//打开homeIcon视图
查看homeIcon = findViewById(android.R.id.home);
//隐藏视图(等图标)
((查看)homeIcon.getParent())setVisibility(View.GONE)。
//指定,我们将在操作栏中显示的标签。
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//设置的ViewPager,将适配器,并设立一个监听器,当
//部分之间的用户刷卡。
mViewPager =(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener()
{
@覆盖
公共无效onPageSelected(INT位置)
{
//当不同的应用程序部分之间刷卡,选择相应的选项卡。
//我们也可以使用ActionBar.Tab#选择()要做到这一点,如果我们有一个参考的选项卡。
actionBar.setSelectedNavigationItem(位置);
}
});
//对于每个应用程序的章节中,添加一个标签,操作栏。
的for(int i = 0; I< mAppSectionsPagerAdapter.getCount();我++)
{
//创建与对应于由适配器中定义的页面标题文本标签。
//还可以指定这个活动对象,它实现了TabListener接口,作为
//监听器当选择该选项卡。
TAB键= actionBar.newTab()的setText(mAppSectionsPagerAdapter.getPageTitle(I))setTabListener(本)。;
actionBar.addTab(标签);
}
}
这是我想要做:
至于用这个意见发生了新的结果。
MainActivity.java
包com.example.android.effectivenavigation;
进口android.app.ActionBar;
进口android.app.ActionBar.Tab;
进口android.app.FragmentTransaction;
进口android.content.Context;
进口android.content.Intent;
进口android.graphics.Color;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentPagerAdapter;
进口android.support.v4.view.ViewPager;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.TextView;
公共类MainActivity扩展FragmentActivity实现ActionBar.TabListener
{
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
//在时间段的viewpager显示器
ViewPager mViewPager;
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
//创建将返回一个片段为三个主要部分的适配器
//应用程序的。
mAppSectionsPagerAdapter =新AppSectionsPagerAdapter(getSupportFragmentManager());
//设置操作栏。
最后的动作条动作条= getActionBar();
//设置自定义动作条
actionBar.setCustomView(R.layout.titlebar);
//显示定制设计的动作条
actionBar.setDisplayShowCustomEnabled(真正的);
//打开homeIcon视图
查看homeIcon = findViewById(android.R.id.home);
//隐藏视图(等图标)
((查看)homeIcon.getParent())setVisibility(View.GONE)。
//指定,我们将在操作栏中显示的标签。
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//设置的ViewPager,将适配器,并设立一个监听器,当
//部分之间的用户刷卡。
mViewPager =(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(新ViewPager.SimpleOnPageChangeListener()
{
@覆盖
公共无效onPageSelected(INT位置)
{
//当不同的应用程序部分之间刷卡,选择相应的选项卡。
//我们也可以使用ActionBar.Tab#选择()要做到这一点,如果我们有一个参考的选项卡。
actionBar.setSelectedNavigationItem(位置);
}
});
/ *最后一个选项卡firstTab = actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(0))
.setTabListener(本)
.setCustomView(R.id.nieuws_tab_layout);
/ *最后一个选项卡secondTab = actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(1))
.setCustomView(R.id.nieuws_tab_layout);
最后一个选项卡thirdTab = actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(2))
.setCustomView(R.id.nieuws_tab_layout);
actionBar.addTab(firstTab);
actionBar.addTab(secondTab);
actionBar.addTab(thirdTab); * /
//对于每个应用程序的章节中,添加一个标签,操作栏。
的for(int i = 0; I< mAppSectionsPagerAdapter.getCount();我++)
{
如果(我== 0)
{
最后查看firstCustomView =新CustomView(本);
//firstCustomView.setBackgroundColor(Color.BLUE);
TAB键= actionBar.newTab().setText(mAppSectionsPagerAdapter.getPageTitle(i)).setTabListener(this).setCustomView(R.layout.nieuws_tab_layout);
actionBar.addTab(标签);
}
其他
{
//创建与对应于由适配器中定义的页面标题文本标签。
//还可以指定这个活动对象,它实现了TabListener接口,作为
//监听器当选择该选项卡。
TAB键= actionBar.newTab()的setText(mAppSectionsPagerAdapter.getPageTitle(I))setTabListener(本)。;
actionBar.addTab(标签);
}
}
}
@覆盖
公共无效onTabUnselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction)
{
}
@覆盖
公共无效onTabSelected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction)
{
// CustomView CTV;
// CTV =新CustomView(背景下,R.attr.tabStyleAttr);
//当选择给定的标签,切换到在ViewPager相应页面。
// LayoutInflater充气=(LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
//查看TabView的= inflater.inflate(R.layout.nieuws_tab_layout,NULL);
//tabView.setBackgroundColor(0xFF00FF00);
//tab.setCustomView(tabView);
mViewPager.setCurrentItem(tab.getPosition());
}
@覆盖
公共无效onTabReselected(ActionBar.Tab选项卡,FragmentTransaction fragmentTransaction)
{
}
公共静态类AppSectionsPagerAdapter扩展FragmentPagerAdapter
{
公共AppSectionsPagerAdapter(FragmentManager FM)
{
超(FM);
}
@覆盖
公共片段的getItem(int i)以
{
开关(ⅰ)
{
情况下0:
//应用程序的第一部分是最有趣的 - 它提供
//一个启动板成这个示例应用程序的其他示威。
返回新LaunchpadSectionFragment();
默认:
//应用程序的其他部分都是假的占位符。
片段片段=新DummySectionFragment();
捆绑的args =新包();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER,I + 1);
fragment.setArguments(参数);
返回片段;
}
}
@覆盖
公众诠释getCount将()
{
返回3;
}
@覆盖
公共CharSequence的getPageTitle(INT位置)
{
开关(位置)
{
情况下0:
{
返回TAB1;
}
情况1:
{
返回TAB2;
}
案例2:
{
返回TAB3;
}
默认:
{
回归第+(位置+ 1);
}
}
}
}
公共静态类LaunchpadSectionFragment扩展片段
{
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
{
查看rootView = inflater.inflate(R.layout.fragment_section_launchpad,集装箱,假);
//集合浏览活动的示范。
rootView.findViewById(R.id.demo_collection_button).setOnClickListener(新View.OnClickListener()
{
@覆盖
公共无效的onClick(视图查看)
{
意向意图=新的意图(getActivity(),CollectionDemoActivity.class);
startActivity(意向);
}
});
//导航到外部活动的示范。
rootView.findViewById(R.id.demo_external_activity).setOnClickListener(新View.OnClickListener()
{
@覆盖
公共无效的onClick(视图查看)
{
//创建一个让用户选择一个照片的意图,但使用
// FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET,确保,重振
//从设备主屏幕上的应用程序不会返回
//到外部的活性。
意图externalActivityIntent =新的意图(Intent.ACTION_PICK);
externalActivityIntent.setType(图像/ *);
externalActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(externalActivityIntent);
}
});
返回rootView;
}
}
/ **
*一个虚拟片段重新presenting应用程序的一部分,但只是显示虚拟文本。
* /
公共静态类DummySectionFragment扩展片段
{
公共静态最后弦乐ARG_SECTION_NUMBER =section_number标;
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState)
{
查看rootView = inflater.inflate(R.layout.fragment_section_dummy,集装箱,假);
捆绑的args = getArguments();
((TextView的)rootView.findViewById(android.R.id.text1))的setText(的getString(R.string.dummy_section_text,args.getInt(ARG_SECTION_NUMBER)));
返回rootView;
}
}
公共类CustomView扩展视图
{
公共CustomView(上下文的背景下)
{
超(背景下,NULL);
}
}
}
tab_layout.xml
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / nieuws_tab_layout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:文本=@字符串/呼号
机器人:重力=center_vertical
机器人:layout_marginTop =15dp
机器人:文字颜色=@机器人:彩色/白
机器人:TEXTSTYLE =黑体
机器人:背景=@机器人:彩色/黑白
/>
< / LinearLayout中>
解决方案
只要设置自定义视图在标签创建时,是这样的:
最后一个选项卡firstTab = actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(0))
.setCustomView(R.id.custom_tab_view_red);
最后一个选项卡secondTab = actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(1))
.setCustomView(R.id.custom_tab_view_blue);
// 等等
actionBar.addTab(firstTab);
actionBar.addTab(secondTab);
// 等等
在 inCreate()
。
你还必须定义的 查看
取值的对应上面的 ID
在你的XML布局文件(而不是风格
S)。
或者,如果你想直接创建视图:
最后查看firstCustomView =新CustomView(本);
firstCustomView.setBackgroundColor(Color.BLUE); //或绘制或资源
最后一个选项卡firstTab = actionBar.newTab()
.setText(mAppSectionsPagerAdapter.getPageTitle(0))
.setCustomView(firstCustomView);
actionBar.addTab(firstTab);
//那么相同的其他选项卡,只需用另一种颜色
离开以下信息,以供参考:的
要定义这样一个观点,你需要指定它在Android 上下文
。这通常是活动
,其中的标签会被显示出来。
假设你初始化选项卡中的活动
,简单地传递活动
实例作为上下文
:
CTV =新CustomView(这一点,R.attr.tabStyleAttr);
如果从活动里面
,或例如:
CTV =新CustomView(getActivity(),R.attr.tabStyleAttr);
如果从片段里面
等。
对于设置特定的样式动作栏选项卡,没有必要去编程创建自定义视图为你想。阅读了有关操作栏第一,然后检查了一点 < A HREF =https://developer.android.com/guide/topics/ui/actionbar.html#StyleExample相对=nofollow>的例子他们提供。正如你所看到的,你就可以在XML中指定的标签样式:
在你的清单文件,您可以应用主题,你的整个应用程序:
&lt;应用机器人:主题=@风格/ CustomActionBarTheme... /&GT;
或单个活动:
&LT;活动机器人:主题=@风格/ CustomActionBarTheme... /&GT;
例如。
对于一个完整的例子完美匹配您的使用情况下,看到这款Android文档文章:的 https://developer.android.com/training/basics/actionbar/styling.html#CustomTabs 。注意国家名单的使用,实现了选中时的风格。
In this link : How do I apply a style programmatically?
Kevin Grant gave a explaination to this question my problem with his code is the context part. To be precise :
ctv = new CustomView(context, R.attr.tabStyleAttr);
In this code it says : context cannot be resolved to a variable
I want to apply specific style to tabs and that's why setting theme doesn't work for me. Any alternative to my problem is welcome too of course.
I try to change the background color, indicator color and text color of actionbar tabs.
@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());
}
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>
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);
}
}
This is what I want to make:
As for the new result using Views this happened
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, 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);
}
});
/*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, 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);
}
}
}
@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, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, 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);
}
}
}
tab_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.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>
解决方案
Just set your custom view at the tab creation time, something like:
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
in inCreate()
.
You'll also have to define View
s corresponding to the above id
s in your xml layout file (and not style
s).
Or, if you want to create the view directly:
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
Leaving the below information for reference:
To define one such view, you need to specify it an Android Context
. This is usually the Activity
where the tabs will be displayed.
Supposing that you initialize your tabs in an Activity
, simply pass the Activity
instance as a Context
:
ctv = new CustomView(this, R.attr.tabStyleAttr);
if from inside the Activity
, or for example:
ctv = new CustomView(getActivity(), R.attr.tabStyleAttr);
if from inside a Fragment
, etc.
As for setting a specific style for action bar tabs, no need to go create a custom view programmatically as you're trying to. Read up a little about the action bar first, then check the example they provide. As you can see, you'll be able to specify the tab style in xml:
In your manifest file, you can apply the theme to your entire app:
<application android:theme="@style/CustomActionBarTheme" ... />
Or to individual activities:
<activity android:theme="@style/CustomActionBarTheme" ... />
for example.
For a complete example matching perfectly your use case, see this Android doc article: https://developer.android.com/training/basics/actionbar/styling.html#CustomTabs . Notice the usage of state-lists to achieve the "when selected style".