我正在使用材质 appcompat v7 为工具栏和菜单抽屉编写简单的代码。
一切在 Nexus 5 上与棒棒糖完美配合,但在棒棒糖之前的版本(4.1 或 4.4)中设备会崩溃。
问题在于定义风格。
如果有人能告诉我问题出在哪里,我就把我的代码贴出来。
这是我的主要活动:
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Hello extends ActionBarActivity {
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
toggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(toggle);
ListView lv_navigation_drawer = (ListView) findViewById(R.id.lv_navigation_drawer);
lv_navigation_drawer.setAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
new String[] {"Screen 1", "Screen 2", "Screen 3"}));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
toggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toggle.onConfigurationChanged(newConfig);
}
}
主要布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/lv_navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
通过简单地扩展 Theme.AppCompat.Light.NoActionBar 来设置样式(我没有为 value-v21 定义样式)
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
</resources>
这一切都可以在棒棒糖和棒棒糖之前的设备上正常工作。当想要自定义工具栏和状态栏的颜色时,问题就来了
我做了这个改变价值观/风格
<resources>
<style name="AppTheme" parent="Base.Theme.AppCompat"/>
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="colorPrimary">#2ecc71</item>
<item name="colorPrimaryDark">#27ae60</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/white</item>
</style>
<color name="primary">#457C50</color>
<color name="primaryDarker">#580C0C</color>
</resources>
我添加了样式价值观-v21
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>
如前所述,这在 Nexus 5 上运行良好,但尽管 UI 具有正确的颜色,但我看不到 ListView 位于菜单抽屉内(我不明白为什么)...但是在“棒棒糖之前”的设备中会崩溃。错误抛出我的是这样的:
...java.lang.IllegalStateException:此活动已经有一个
由窗口装饰提供的操作栏。不要求
Window.FEATURE_ACTION_BAR 并将 windowActionBar 设置为 false
主题改为使用工具栏。
我搜索了很多有关此错误的信息并尝试了很多选项:主活动中样式内的项目 windowActionBar 为“false”,已变为“Toolbar”到“android.support.v7.widget.Toolbar 工具栏”,但没有成功。 ..
我还搜索了已经给出的示例,但不适合我
我使用的是Eclipse,manifyt的目标是21,最小是16,我还更新了sdk和adt ...
任何人都可以帮助在棒棒糖之前的设备上正常工作吗?