gpt4 book ai didi

java - 菜单项未显示在工具栏 SDK 29 中

转载 作者:行者123 更新时间:2023-12-02 01:44:20 25 4
gpt4 key购买 nike

我遇到的问题是菜单按钮未显示在工具栏中。我设置了一个按钮始终显示,另一个按钮仅在有房间时显示。没有显示。

这是使用 Android SDK 29 (Q) 编译的。

(我在 Stack Overflow 上搜索过类似的问题,但其他问题至少是几年前写的,并且使用的是较旧的 SDK。)

enter image description here

activity_main.xml

   <?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/tealBlue"
tools:context=".view.MainActivity">

<androidx.appcompat.widget.Toolbar
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/toolbar_top_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
tools:menu="@menu/top_menu_overflow"
/>

<ExpandableListView
android:id="@+id/expandableListViewPolls"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:indicatorLeft="?
android:attr/expandableListPreferredItemIndicatorLeft"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/toolbar_top_main"

tools:layout_editor_absoluteX="16dp" />
...

top_menu_overflow.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item android:id="@+id/action_start_voting"
android:title="@string/start_voting_text"
app:showAsAction="always"/>
<item android:id="@+id/action_see_voting_results"
android:title="@string/see_voting_results_text"
app:showAsAction="ifRoom"/>
</menu>

MainActivity.java

   import androidx.appcompat.widget.Toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// setup toolbar
Toolbar myToolbar = findViewById(R.id.toolbar_top_main);
setSupportActionBar(myToolbar);
}

样式.xml

   <resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...

AndroidManifest.xml

  <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.votetodo">

<application
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
...

构建.gradle

apply plugin: 'com.android.application'

android {
compileSdkVersion 29
buildToolsVersion "29.0.1"
defaultConfig {
applicationId "com.example.votetodo"
minSdkVersion 15
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true


javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation":
"$projectDir/schemas".toString()]
}
}

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

dataBinding {
enabled = true
}
}

dependencies {
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'


implementation 'com.google.android.material:material:1.0.0'

// room
def room_version = "2.2.0-alpha01"
implementation "androidx.room:room-rxjava2:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"


// rxandroid
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"

}

最佳答案

发现问题了。我需要在 MainActivity.java 中添加以下方法才能膨胀菜单项。

@Override public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.top_menu_overflow, menu);
return true;
}

关于java - 菜单项未显示在工具栏 SDK 29 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57472000/

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