我想将默认的蓝色更改为其他任何内容......
经过一番研究,我尝试使用网站 Android Holo Colors Generator 。我从网站上下载了文件,然后将它们添加到我的Android应用程序中,但是我遇到了两个错误:
- Attribute "divider" has already been defined attrs.xml, .../res/values, line 6 Android AAPT Problem
- Error: No resource found that matches the given name (at 'drawable' with value '@color/transparent'). item_background_holo_light.xml, ../res/drawable, line 25 Android AAPT Problem
我试图评论这两行,但没有应用任何更改。有什么建议或帮助吗?
答案 0 :(得分:2)
您得到的错误是因为您的项目或引用的项目(如acionbarSherlock)具有已使用该名称的属性。您可以使用以下步骤解决此问题
1)随意注释掉该属性
2)定义自定义分隔线 R.drawable.customDivider :
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size android:width="1dip" />
<solid android:color="#666666" />
</shape>
3)使用setDividerDrawable
设置
mTabHost.getTabWidget().setDividerDrawable(getResources().getDrawable(R.drawable.customDivider));
注意: android:divider 布局属性仅在Android Honeycomb或更高版本中可用,因此我们需要以编程方式设置。
holo generator site在实现其工件时信息量不大,但资源非常方便。请参阅下面的完整实施。
注意 另一种选择是使用Holo Everywhere library进行蜂窝前样式支持。
将下载的文件添加到项目中后,您需要实现类似下面的内容。
注意:这些标签在片段
内实施有条件地应用下载的指标:
public class TabsFrag extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentTabHost retval = (FragmentTabHost)inflater.inflate(R.layout.home_tabbed_view, container, false);
return retval;
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentTabHost mTabHost = (FragmentTabHost)getView().findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getActivity().getSupportFragmentManager(), R.id.realtabcontent);
//This is for stying to be the same across SDKs, we are supporting pre.
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
{
mTabHost.getTabWidget().setDividerDrawable(getResources().getDrawable(R.drawable.customDivider));
View tab1Indicator = getActivity().getLayoutInflater().inflate(R.layout.tab_indicator_holo, mTabHost.getTabWidget(), false);
View tab2Indicator = getActivity().getLayoutInflater().inflate(R.layout.tab_indicator_holo, mTabHost.getTabWidget(), false);
((TextView) tab1Indicator.findViewById(android.R.id.title)).setText("Tab 1 label");
mTabHost.addTab(mTabHost.newTabSpec("TAB1").setIndicator(tab1Indicator),
MenuHomeDashboard.class, null);
((TextView) tab2Indicator.findViewById(android.R.id.title)).setText("Tab 2 label");
mTabHost.addTab(mTabHost.newTabSpec("TAB2").setIndicator(tab2Indicator),
MenuHomeNewsFeed.class, null);
}
else //No need to use the generated style unless you want to.
{
mTabHost.addTab(mTabHost.newTabSpec("TAB1").setIndicator("Tab 1 label"),
MenuHomeDashboard.class, null);
mTabHost.addTab(mTabHost.newTabSpec("TAB2").setIndicator("Tab 2 label"),
MenuHomeNewsFeed.class, null);
}
}
}
您的自定义标签布局: R.home_tabbed_view
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="@android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
您应该从全息颜色生成器接收 R.layout.tab_indicator_holo 布局
<!-- Copyright (C) 2011 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="@dimen/tab_host_default_height"
android:orientation="horizontal"
style="@style/TabAppTheme"
android:gravity="center">
<ImageView
android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:visibility="gone"
android:contentDescription="@null" />
<TextView
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
style="@style/TabTextAppTheme" />
</LinearLayout>