如何在安卓的操作栏中创建按钮

2022-09-01 04:06:35

我正在开发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>

B2B 交付活动.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


答案 1

在作业类中,如果默认情况下不存在以下方法,请重写这些方法:

// create an action bar button
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // R.menu.mymenu is a reference to an xml file named mymenu.xml which should be inside your res/menu directory. 
    // If you don't have res/menu, just create a directory named "menu" inside res
    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

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

答案 2

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

  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. 在样式中.xml添加以下代码:

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

推荐