2017-03-01 48 views
0

我試圖在Android Studio中爲我的應用程序設置操作欄。 我一直在關注Google教程,但出於某種原因,我在欄上放置的物品在運行應用程序時並未顯示。但是,它們確實出現在設計窗口中。Android Studio:操作欄圖標顯示在設計窗口中,但不在運行應用程序時

Image of the design window

Image of the app

這是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

操作欄已被棄用,你應該使用工具欄,但顯示在那裏你膨脹的菜單操作欄 – tyczj

+0

把你的活動或片段完整代碼 – akhilesh0707

+0

將java代碼粘貼到here.Are使用setHasOptionsMenu(true)並覆蓋onCreateOptionsMenu? –

回答

1

你永遠膨脹的菜單,你需要重寫onCreateOptionsMenu

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.my_menu, menu); 
    return true; 
} 
+0

完美,謝謝你! – Jashani

相關問題