我正试图在Android工作室中为我的应用设置操作栏。 我一直在关注Google教程,但出于某种原因,我在运行应用时没有显示我放在栏上的项目。但它们确实出现在设计窗口中。
这是从Google教程中复制的XML
<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="@drawable/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>
这里是主要活动的完整代码,我在其底部尝试使用操作栏项。
package php.comget_all.ipeelu.httpweb_service_test.intenseapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Stack;
public class MainActivity extends AppCompatActivity {
private Button sendButton;
public EditText editText;
public Stack<TableRow> messageStack;
public LinearLayout messagesDisplayLayout;
public ScrollView scroller;
private TextView response;
private EditText editTextAddress, editTextPort;
private Button buttonConnect, buttonClear;
private Client client;
// Boolean telling us whether a download is in progress, so we don't trigger overlapping
// downloads with consecutive button clicks.
private boolean mDownloading = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
client = new Client("10.0.2.2", 12345, this);
client.execute();
messagesDisplayLayout = (TableLayout) findViewById(R.id.messageDisplay);
sendButton = (Button) findViewById(R.id.sendButton);
editText = (EditText) findViewById(R.id.edit_message);
scroller = (ScrollView) findViewById(R.id.scroller);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() != 0)
sendButton.setEnabled(true);
else
sendButton.setEnabled(false);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void afterTextChanged(Editable s) {}
});
messageStack = new Stack<TableRow>();
}
/** Called when the user clicks the Send button */
public void sendMessage(View view)
{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm");
String ts = simpleDateFormat.format(new Date());
String message = editText.getText().toString();
TextView messageTV = new TextView(this);
messageTV.setText(ts + ": " + message);
messageTV.setTextSize(20);
TableRow messageRow = new TableRow(this);
messageRow.setPadding(0,20,0,0);
messageRow.addView(messageTV);
messageTV.getLayoutParams().width = LinearLayout.LayoutParams.WRAP_CONTENT;
messageTV.getLayoutParams().height = LinearLayout.LayoutParams.WRAP_CONTENT;
messagesDisplayLayout.addView(messageRow);
messageStack.push(messageRow);
editText.getText().clear();
scroller.fullScroll(scroller.FOCUS_DOWN);
//client.sendMessage(message);
synchronized (client.thingy)
{
client.message = message;
client.thingy.notify();
}
}
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);
}
}
}
对不起,如果我犯了任何新手的错误,我可能会像Android Studio一样陌生。不过我已经看了这个,并且无法在任何地方找到合适的答案,所以这是我最后的选择。
答案 0 :(得分:1)
您永远不会夸大菜单,您需要覆盖onCreateOptionsMenu
实施例
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}