gpt4 book ai didi

java - Android 添加操作按钮

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

好的,在我开始之前,我只想告诉你们我有 A​​ndroid Studio 2.1 5 预览版。所以我遇到的问题是添加操作按钮。我按照官方开发文档中的本教程进行操作:

http://developer.android.com/intl/es/training/appbar/actions.html#add-actions

所以在我学习了教程之后,我得到了这段代码:

MainActivity.java:

package com.example.amanuel.actionbar;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.ToolbarWidgetWrapper;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.my_toolbar);
toolbar.setLogo(R.mipmap.ic_launcher);
toolbar.setLogoDescription("Logo");
setSupportActionBar(toolbar);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// User chose the "Settings" item, show the app settings UI...
return true;

case R.id.action_favorite:
// User chose the "Favorite" action, mark the current item
// as a favorite...
return true;

default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);

}
}
}

res/menu/menu.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">
<!-- "Mark Favorite", should appear as action button if possible -->
<item
android:id="@+id/action_favorite"
android:icon="@mipmap/ic_favorite_black_48dp"
android:title="@string/action_favorite"
app:showAsAction="ifRoom"/>

<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never"/>
</menu>

AndroidManifest.xml:

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.amanuel.actionbar.MainActivity">

<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>

所以当我把所有这些放在 genymotion 上运行时,我得到了这个:

enter image description here

好吧,我得到了我的操作栏,但是我的操作按钮到底在哪里?就像它在开发文档中向我展示的那样:

enter image description here

我也不明白为什么操作栏位于中间。它通常应该位于顶部,就像普通的工具栏一样。这让我很困惑,请帮助。谢谢!

最佳答案

您没有任何内容引用您的 menu.xml 资源。

重写onCreateOptionsMenu(),并使用getMenuInflater().inflate(menu, R.menu.menu)

关于java - Android 添加操作按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36535701/

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