大家好我正在制作一个顶部有Tabhost / Tabtoolbar的应用程序,它下面还有查看器页面。我的toolbar_layout.xml
文件中面临错误
错误显示在行android:minHeight="?android:attr/actionBarsize
中,原因为:
找不到与给定名称匹配的资源
我每次在应用程序中创建一个新的Fragment java类和Fragment xml文件时都会遇到此错误
下面我有我的代码。 Plz帮助我解决这个错误,并使用Android 5.1 for version在我的Android工作室中执行此操作
提前致谢
工具栏布局xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:minHeight="?attr/actionBarsize"
android:background="?attr/colorPrimary"
android:fitsSystemWindows="true"
android:id="@+id/toolbar"
app:theme="@style/Base.ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
主xml文件
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
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"
tools:context="com.example.mohammadzakriya.tabhost2.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/toolbar_layout"/>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tabLayout"
app:tabMode="fixed"
app:tabGravity="fill"
></android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewPager"></android.support.v4.view.ViewPager>
</RelativeLayout>
MainAcivity java文件
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar =(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabLayout =(TabLayout)findViewById(R.id.tabLayout);
viewPager =(ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new HomeFragment(),"Home");
viewPagerAdapter.addFragments(new Database(),"Database");
viewPagerAdapter.addFragments(new contact(),"contact");
tabLayout.setupWithViewPager(viewPager);
}
}
ViewPage适配器
public class ViewPagerAdapter extends FragmentPagerAdapter{
ArrayList<Fragment> fragments = new ArrayList<>();
ArrayList<String> tabTitles = new ArrayList<>();
public void addFragments(Fragment fragments,String titles){
this.fragments.add(fragments);
this.tabTitles.add(titles);
}
public ViewPagerAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
}
}
我的build.gradle(模块:app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "com.example.mohammadzakriya.tabhost2"
minSdkVersion 22
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:design:24.1.1'
}
答案 0 :(得分:0)
使用android:minHeight="?android:actionBarSize"
。
答案 1 :(得分:0)
改变这个:
android:minHeight="?attr/actionBarsize"
要:
android:minHeight="?attr/actionBarSize"
请注意,actionBarSize
为S