【发布时间】:2015-03-10 06:41:48
【问题描述】:
在阅读了 SO 线程 here 和 android 博客文章 here 之后,我需要使用 AppCompat V7:21 中引入的新工具栏功能。我将博文中的 Toolbar sn-p 完全复制到了我的布局中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar <- Line 8
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
</LinearLayout>
问题是我得到了:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blabla.PrefActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class <unknown>
有趣的是,如果我删除这一行: android:minHeight="?attr/actionBarSize"
它有效。所以问题不是因为项目依赖等等。看来我只能访问 ?attr 元素,如 actionBarSize 或 colorPrimary
不用说我已经添加了 AppCompat 依赖。并且activity继承自PreferenceActivity。这是我的毕业典礼:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-v4:21.0.3'
compile 'com.android.support:appcompat-v7:21.0.3'
}
here 和 here 有人报告说随机解决了这个问题。但是我的随机数在过去两天都没有用。
编辑:
这是我的活动:
public class PrefActivity extends PreferenceActivity {
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
LinearLayout content = (LinearLayout) root.getChildAt(0);
LinearLayout toolbarContainer = (LinearLayout) View.inflate(this, R.layout.activity_prefs, null);
root.removeAllViews();
toolbarContainer.addView(content);
root.addView(toolbarContainer);
mToolbar = (Toolbar) toolbarContainer.findViewById(R.id.toolbar);
selectResource();
mToolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
【问题讨论】:
-
你展示了完整的布局文件吗?
-
尝试使用
android:minHeight="?android:attr/actionBarSize" -
@VipulKumar 不仅不能解决,而且我需要与 API14 兼容
-
我宁愿使用
android:layout_height="?attr/actionBarSize",如果对你没有区别的话。 -
@miav 是的,我也会这样做。但我保持工具栏 sn-p 与 android 博客中的完全相同。问题解决后,我将添加自己的属性。
标签: android android-actionbar toolbar android-appcompat