- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
在此链接中:How do I apply a style programmatically?
Kevin Grant 对这个问题做了解释,他的代码的问题是上下文部分。准确地说:
ctv = new CustomView(context, R.attr.tabStyleAttr);
在这段代码中它说:上下文无法解析为变量
我想将特定样式应用于选项卡,这就是设置主题对我不起作用的原因。当然,也欢迎任何替代我的问题的方法。
我尝试更改操作栏选项卡的背景颜色、指示器颜色和文本颜色。
@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());
}
样式.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);
}
}
这是我想做的:
至于使用 Views 的新结果,这发生了
主 Activity .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>
最佳答案
只需在创建选项卡时设置您的自定义 View ,例如:
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
在 inCreate()
中。
您还必须在 xml 布局文件中定义与上述 id
对应的 View
(而不是 样式
s).
或者,如果您想直接创建 View :
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
留下以下信息供引用:
要定义一个这样的 View ,您需要为它指定一个 Android Context
。这通常是将显示选项卡的 Activity
。假设您在 Activity
中初始化选项卡,只需将 Activity
实例作为 Context
传递即可:
ctv = new CustomView(this, R.attr.tabStyleAttr);
如果来自 Activity
内部,或者例如:
ctv = new CustomView(getActivity(), R.attr.tabStyleAttr);
如果来自 Fragment
等
至于为操作栏选项卡设置特定样式,无需像您尝试的那样以编程方式创建自定义 View 。仔细阅读 about the action bar首先,然后检查the example他们提供。如您所见,您将能够在 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" ... />
例如。
有关与您的用例完美匹配的完整示例,请参阅这篇 Android 文档文章:https://developer.android.com/training/basics/actionbar/styling.html#CustomTabs .请注意使用状态列表来实现“选择时样式”。
关于android - 如何自定义单个选项卡? (更改背景颜色、指示器颜色和文本颜色),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21335267/
我正在阅读 java swing,但在理解它时遇到问题。 Color 是一个类吗? Color[] col= {Color.RED,Color.BLUE}; 这在java中是什么意思? 最佳答案 Is
我正在研究用 python 编写的 pacman 程序。其中一个模块是处理吃 bean 游戏的图形表示。这当然是一些主机颜色。列表如下: GHOST_COLORS = [] ## establishe
本网站:http://pamplonaenglishteacher.com 源代码在这里:https://github.com/Yorkshireman/pamplona_english_teache
我最近将我的手机更新为 Android Marshmallow 并在其上运行了我现有的应用程序,但注意到颜色行为有所不同:将更改应用到 View (可绘制)的背景时,共享相同背景的所有 View (引
所有 X11/w3c 颜色代码在 Android XML 资源文件格式中是什么样的? I know this looks a tad ridiculous as a question, but giv
试图让 ffmpeg 创建音频波形,同时能够控制图像大小、颜色和幅度。我已经尝试过这个(以及许多变体),但它只是返回无与伦比的 "。 ffmpeg -i input -filter_complex "
我很好奇你是否有一些关于 R 中颜色酿造的技巧,对于许多独特的颜色,以某种方式使图表仍然好看。 我需要大量独特的颜色(至少 24 种,可能需要更多,~50 种)用于堆叠区域图(所以不是热图,渐变色不起
我看到的许多 WPF 示例和示例似乎都有硬编码的颜色。这些指南 - http://msdn.microsoft.com/en-us/library/aa350483.aspx建议不要硬编码颜色。在构建
我想更改文件夹的默认蓝色 如何设置? 最佳答案 :hi Directory guifg=#FF0000 ctermfg=red 关于Vim NERDTree 颜色,我们在Stack Overflow上
是否有关于如何将任意字符串哈希为 RGB 颜色值的最佳实践?或者更一般地说:3 个字节。 你问:我什么时候需要这个?这对我来说并不重要,但想象一下任何 GitHub 上的那些管图 network pa
我正在尝试将默认颜色设置为自定义窗口小部件。 这是有问题的代码。 class ReusableCard extends StatelessWidget { ReusableCard({this.
import javax.swing.*; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.Ta
我有一个 less 文件来定义一堆颜色/颜色。每个类名都包含相关颜色的名称,例如 .colourOrange{..} 或 .colourBorderOrange{..} 或 navLeftButtOr
我有一个RelativeLayout,我需要一个黑色背景和一个位于其中间的小图像。我使用了这段代码: 其中@drawable/bottom_box_back是: 这样我就可以将图像居中了。但背
我需要设置 浅色 的 JPanel 背景,只是为了不覆盖文本(粗体黑色)。 此刻我有这个: import java.util.Random; .... private Random random =
我正在尝试制作一个自定义文本编辑器,可以更改特定键入单词的字体和颜色。如何更改使用光标突出显示的文本的字体和/或颜色? 我还没有尝试过突出显示部分。我尝试获取整个 hEdit(HWND) 区域并更改字
我想改变我整个应用程序的颜色。 在我的 AndroidManfiest.xml 中,我有正确的代码: 在 values 文件夹中,我有 app_theme.xml: @style/MyAc
是否可以使用 android 数据绑定(bind)从 xml 中引用颜色? 这很好用: android:textColor="@{inputValue == null ? 0xFFFBC02D : 0
有没有办法在 Android 应用程序中设置“空心”颜色? 我的意思是我想要一个带有某种背景的框,而文本实际上会导致背景透明。换句话说,如果整个 View 在蓝色背景上,文本将是蓝色的,如果它是红色的
我用CGContextStrokePath画在白色背景图片中的一条直线上,描边颜色为红色,alpha为1.0画线后,为什么点不是(255, 0, 0),而是(255, 96, 96)为什么不是纯红色?
我是一名优秀的程序员,十分优秀!