阅读SO线程here和android博客文章here之后,我需要使用AppCompat V7:21中引入的新工具栏功能。我从博客文章中准确地将工具栏摘要复制到了我的布局中:
<?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依赖项。该活动是从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();
}
});
}
最佳答案
使用属性(?attr /)意味着使用当前上下文主题中的值。您需要确保您的主题具有?attr / actionBarSize属性的价值。您可以从@ style / Theme.AppCompat使用或继承主题。或者只是手动设置值。
关于android - AppCompat v7工具栏“错误夸大类<未知>”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27903000/