我在我的活动中添加了独立的工具栏,它显示但是没有显示我为它定义的颜色。我正在使用的代码有什么问题?
以下是我正在使用的代码。
activity_categories.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:weightSum="10"
android:background="#D32F2F"
tools:context="com.example.knowledgeup.LoginActivity" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:elevation="4dp"
style="@style/ToolbarTheme" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="8" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</LinearLayout>
这是我如何将它添加到我在java文件中的活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categories);
mToolbar = (Toolbar)findViewById( R.id.toolbar );
mToolbar.setTitle(R.string.title_activity_categories);
}
这是我的styles.xml
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
<style name="ToolbarTheme" parent="Theme.AppCompat">
<item name="colorPrimary">@color/material_blue_grey_900</item>
<item name="colorPrimaryDark">@color/color_toolbar</item>
</style>
</resources>
这是它的表现方式
答案 0 :(得分:1)
试试这个
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:elevation="4dp"
android:background = "@color/color_toolbar"
style="@style/ToolbarTheme" />
在activity_categories.xml
或在styles.xml
<style name="ToolbarTheme" parent="Theme.AppCompat">
<item name="colorPrimary">@color/material_blue_grey_900</item>
<item name="colorPrimaryDark">@color/color_toolbar</item>
<item name="android:background">@color/color_toolbar</item>
</style>
答案 1 :(得分:0)
使用以下属性更改工具栏颜色。
public class MyActivity extends FragmentActivity implements AdapterView.OnItemClickListener{
ViewPager viewPager = null;
int mSelectedItemPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_fail_activity);
viewPager = (ViewPager) findViewById(R.id.fail_pager_VP);
viewPager.setAdapter(new pagerAdapter(getSupportFragmentManager()));
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mSelectedItemPosition = position;
viewPager.setCurrentItem(1);
FragmentStatePagerAdapter fspa = (FragmentStatePagerAdapter) viewPager.getAdapter();
Fragment_B item = (Fragment_B) fspa.getItem(1);
item.setData(position);
item.refresh();
}
}
class pagerAdapter extends FragmentStatePagerAdapter {
public pagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = null;
if (position == 0) {
fragment = new Fragment_A();
} else if (position == 1) {
fragment = new Fragment_B();
}
return fragment;
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
String title = "";
if (position == 0) {
return "a_title";
}else if (position == 1) {
return "b_title";
}
}
}
public class Fragment_A extends android.support.v4.app.Fragment {
private ListView frag_a_listView;
DataAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ListView frag_a_listView = (ListView) getActivity().findViewById(R.id.frag_a_LV);
frag_a_listView.setOnItemClickListener((AdapterView.OnItemClickListener) getActivity());
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_fail_main, container, false);
frag_a_listView = (ListView) layout.findViewById(R.id.frag_a_LV);
dataAdapter = new DataAdapter(getActivity(),
R.layout.data_row,
R.id.data_label_TV,
getFrag_aData());
frag_a_listView.setAdapter(dataAdapter);
return layout;
}
private List<String> getFrag_aData() {
...
return someData;
}
答案 2 :(得分:0)
您只需在工具栏中设置背景。
egCode Snippet
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/action_bar_bg"
android:elevation="@dimen/elevation_button_default"
android:theme="@style/ThemeOverlay.AppCompat.Dark" />