gpt4 book ai didi

java - 抽屉式导航 - 错误 : failed linking file resources

转载 作者:行者123 更新时间:2023-11-30 05:34:20 24 4
gpt4 key购买 nike

这是我的主要布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBFBD8">
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
android:id="@+id/nv">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

第二个布局文件:

<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#816291">
<ImageView
android:id="@+id/imageView"
android:layout_width="180dp"
android:layout_height="130dp"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp"
android:background="@drawable/man" />
</LinearLayout>

第三个布局文件:

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content">


<item android:id="@+id/account"
android:icon="@drawable/icon1"
android:title="My Account"/>
<item android:id="@+id/settings"
android:icon="@drawable/icon2"
android:title="Settings"/>
<item android:id="@+id/mycart"
android:icon="@drawable/icon3"
android:title="My Cart"/>
</menu>

这是我的 MainActivity.java:

package com.example.exercise1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;




import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
import

import com.google.android.material.navigation.NavigationView;


public class MainActivity extends AppCompatActivity {

private DrawerLayout dl ;
private ActionBarDrawerToggle abdt;
private NavigationView nv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

dl = findViewById(R.id.activity_main);
abdt = new ActionBarDrawerToggle(this, dl, 0, 0);
dl.addDrawerListener(abdt);
abdt.syncState();getSupportActionBar().setDisplayHomeAsUpEnabled(true);
nv = findViewById(R.id.nv);
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
switch(id) {

case R.id.account:
Toast.makeText(MainActivity.this, "My Account", Toast.LENGTH_SHORT).show();
return true;

case R.id.settings:
Toast.makeText(MainActivity.this, "Settings", Toast.LENGTH_SHORT).show();
return true;

case R.id.mycart:
Toast.makeText(MainActivity.this, "My Cart", Toast.LENGTH_SHORT).show();
return true;

default:
return true;
}
}
});


}

public boolean onOptionsItemSelected(MenuItem item) {

if(abdt.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
}

和build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "com.example.exercise1"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {

implementation 'com.android.support:design:28.0.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

在编译过程中,我收到消息:错误:链接文件资源失败。附加信息:错误:找不到资源菜单/navigation_menu(又名com.example.exercise1:menu/navigation_menu)。我应该怎么做才能修复这个错误?

最佳答案

确保此“第三个布局文件”名为src/main/res/menu/navigation_menu.xml

此外,属性 layout_heightlayout_width 对于菜单资源(这可能是崩溃的原因)...和 ​​com.android 来说是无用的。 support:design:28.0.0 应迁移到 com.google.android.material:material:1.0.0

关于java - 抽屉式导航 - 错误 : failed linking file resources,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56910703/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com