Android操作栏,添加操作按钮

时间:2015-07-30 01:01:53

标签: android button menu android-actionbar

我正试图在没有运气的情况下向操作栏添加按钮。没有错误。按钮位于@drawable中。但它没有显示出来。 我做错了什么?

(它要求我添加更多详细信息,但我没有其他内容可以添加)

测试活动:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;


public class test extends ActionBarActivity  {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        setupCREATEbut();
    }

    private void setupCREATEbut() {
    Button CREATEbut = (Button) findViewById (R.id.appenter2);
    CREATEbut.setOnClickListener(new View.OnClickListener(){

       @Override
       public void onClick(View v){
            Intent intent = new Intent(v.getContext(), MainActivity.class);
            startActivityForResult(intent,0);
        }
        });
    }

 @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         MenuInflater inflater = getMenuInflater();
         inflater.inflate(R.menu.topmenu, menu);
         return super.onCreateOptionsMenu(menu);
     }  
 }

测试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"
tools:context="rs.test">

    <LinearLayout
         android:id="@+id/centerblock"
         android:layout_width="400dp"
         android:layout_height="wrap_content"
         android:layout_centerHorizontal="true"
         android:layout_centerVertical="true"
         android:orientation="vertical" >

         <RelativeLayout
             android:id="@+id/relcenterblock"
             android:layout_width="fill_parent"
             android:layout_height="415dp" >

               <Button
                  android:id="@+id/appenter2"
                  android:layout_width="160dip"
                  android:layout_height="160dip"
                  android:layout_centerHorizontal="true"
                  android:layout_centerVertical="true"
                  android:background="@drawable/bigroundbutton"
                  android:gravity="center"
                  android:text="@string/request"
                  android:textColor="#ffffff"
                  android:textSize="30sp" />
          </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

topmenu xml:

 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
 <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_toprightmenu"
      android:title="@string/toprightmenu"/> 

  </menu>

1 个答案:

答案 0 :(得分:2)

尝试使用showAsAction=alwaysshowAsAction=ifRoom

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:yourapp="http://schemas.android.com/apk/res-auto">
 <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_toprightmenu"
      android:title="@string/toprightmenu"
      yourapp:showAsAction="always"/> 
</menu>