如何在android中的Action栏中创建按钮

时间:2016-07-02 10:10:02

标签: java android xml button android-actionbar

我正在开发Android应用程序,我需要一个操作栏中的按钮。 这些是我的活动和java文件。 所以,我需要一个工具栏右侧的按钮。 提前谢谢。

content_b2_bdeliveries.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.ideabiz.riderapplication.ui.B2BDeliveriesActivity"
tools:showIn="@layout/activity_b2_bdeliveries">
//remaining code;
</LinearLayout>

activity_b2_bdeliveries.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ideabiz.riderapplication.ui.B2BDeliveriesActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_b2_bdeliveries" />

</android.support.design.widget.CoordinatorLayout>

B2BDeliveriesActivity.java:

public class B2BDeliveriesActivity extends AppCompatActivity {
private final String TAG_NAME="B2BActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_b2_bdeliveries);
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    ActionBar actionBar = getSupportActionBar();
    //Log.d(TAG_NAME,actionBar.toString());
    if (actionBar != null) {
        try {
            actionBar.setDisplayHomeAsUpEnabled(true);
            } catch (Exception e) {
                Crashlytics.logException(e);
                Log.d(TAG_NAME, "Null Pointer from setDisplayHomeAsUpEnabled" + e.getMessage());
                Utilities.issueTokenUpload("Null Pointer from setDisplayHomeAsUpEnabled" + e, sharedPreferences.getInt("tripId", 999999), sharedPreferences.getString("driverPhoneNo", "9999999999"), sharedPreferences.getString("driverName", "default"), getResources().getString(R.string.version));
            }
    }
}

sample requirement picture

5 个答案:

答案 0 :(得分:51)

// create an action bar button
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.mymenu, menu);
    return super.onCreateOptionsMenu(menu);
}

// handle button activities
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.mybutton) {
    // do something here
    }
    return super.onOptionsItemSelected(item);
}

并且你的mymenu.xml应该在res / menu目录下(如果你没有res / menu,只需在res里面创建一个名为“menu”的目录),看起来像这样:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/mybutton"
    android:title=""
    app:showAsAction="always"
    android:icon="@drawable/mybuttonicon"
    />
</menu>

答案 1 :(得分:6)

如果要自定义ActionBar的UI,则可以将工具栏用作ActionBar。以下是执行相同操作的步骤:

  1. 在活动布局xml中将以下代码添加为AppBarLayout的子代。

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="1">
    
                <TextView
                    android:id="@+id/title"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="ActionBar Title"/>
    
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
    
            </LinearLayout>
    
        </android.support.v7.widget.Toolbar>
    
  2. 通过在Activity onCreate()方法中添加以下代码,将工具栏设为ActionBar。

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
  3. 在AndroidManifest.xml中,将以下主题添加到您的活动中:

    android:theme="@style/AppTheme.NoActionBar"
    
  4. 在styles.xml中添加以下代码:

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    

答案 2 :(得分:3)

有人在

之前回答了这个问题

您有两个步骤:

  1. 在res / menu中创建菜单(将其命名为my_menu.xml)

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_cart"
        android:icon="@drawable/cart"
        android:orderInCategory="100"
        android:showAsAction="always"/> 
    </menu>
    
  2. 覆盖onCreateOptionsMenu和充气菜单

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return true;
    }
    
  3. 访问:How to add button in ActionBar(Android)?

答案 3 :(得分:0)

https://developer.android.com/guide/topics/ui/menus.html#xml正确描述了它。您可以在&#34; menu&#34;创建一个menu.xml文件。目录,与&#34; drawable&#34;处于同一级别和&#34;布局&#34;目录

答案 4 :(得分:0)

res/menu目录中的菜单创建一个xml文件(在本例中为menu_main.xml):

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.rashish.myapplication.MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="Button title"
        android:icon="@drawable/buttonicon"
        app:showAsAction="never" />
</menu>

然后在您的活动类中添加这些方法:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here.
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        //process your onClick here
        return true;
    }

    return super.onOptionsItemSelected(item);
}
相关问题