工具栏 SDK 29 中未显示的菜单项
Menu items not showing inToolbar SDK 29
我遇到的问题是菜单按钮没有显示在工具栏中。我将一个按钮设置为始终显示,将一个按钮设置为仅在有空间时显示。 None 正在显示。
这是用 Android SDK 29 (Q) 编译的。
(我在 Stack Overflow 上搜索过类似的问题,但其他问题至少是几年前写的,并且使用的是较旧的 SDK。)
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);
}
styles.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"
...
build.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;
}
我遇到的问题是菜单按钮没有显示在工具栏中。我将一个按钮设置为始终显示,将一个按钮设置为仅在有空间时显示。 None 正在显示。
这是用 Android SDK 29 (Q) 编译的。
(我在 Stack Overflow 上搜索过类似的问题,但其他问题至少是几年前写的,并且使用的是较旧的 SDK。)
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);
}
styles.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"
...
build.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;
}