0
我试图在Android Studio中为我的应用程序设置操作栏。 我一直在关注Google教程,但出于某种原因,我在栏上放置的物品在运行应用程序时并未显示。但是,它们确实出现在设计窗口中。Android Studio:操作栏图标显示在设计窗口中,但不在运行应用程序时
这是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一样新手。然而,我已经查看了这个,并且无法找到任何有效的答案,所以这是我的最后一招。
操作栏已被弃用,你应该使用工具栏,但显示在那里你膨胀的菜单操作栏 – tyczj
把你的活动或片段完整代码 – akhilesh0707
将java代码粘贴到here.Are使用setHasOptionsMenu(true)并覆盖onCreateOptionsMenu? –