【发布时间】:2016-12-05 17:50:03
【问题描述】:
大家好,我正在制作一个顶部有 Tabhost/Tabtoolbar 的应用程序,它的下方还有查看器页面。我的toolbar_layout.xml 文件中面临错误
错误显示在android:minHeight="?android:attr/actionBarsize 行中,原因是:
找不到与给定名称匹配的资源
每次在应用程序中创建新的 Fragment java 类和 Fragment xml 文件时都会遇到此错误
下面是我的代码。请帮我解决这个错误,并在我的 Android 工作室中使用 Android 5.1 版本进行此操作
提前致谢
工具栏布局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(模块:应用程序)
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'
}
【问题讨论】:
-
Java !== JavaScript