为了账号安全,请及时绑定邮箱和手机立即绑定

有没有一种简单的方法可以将按钮添加到子活动的操作栏?

有没有一种简单的方法可以将按钮添加到子活动的操作栏?

红颜莎娜 2023-03-09 10:43:36
我正在尝试将按钮添加到 android studio 应用程序中的操作栏到子活动,但我只设法将它添加到主要活动。有人知道我该怎么做吗?我希望“确定”按钮位于我用橙色绘制的位置。带有操作栏的主要活动。带操作栏的儿童活动
查看完整描述

3 回答

?
蓝山帝景

TA贡献1843条经验 获得超7个赞

您应该为您的活动创建一个菜单:

里面res -> menu选择new -> Menu资源文件

//img1.sycdn.imooc.com//6409481b0001118b06540778.jpg

在您的新菜单 xml 文件中写入以下内容:


<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item

        android:id="@+id/menu_action_ok"

        android:title="Ok"

        app:showAsAction="ifRoom" />

</menu>

基本上,您正在创建一个菜单,其中包含一个名为“确定”的选项,但如果需要,您可以有更多选项。如果需要,您还可以自定义此选项的视图:


app:actionLayout="@layout/filter_ka"

在您的父活动中:


@Override

public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu.

    getMenuInflater().inflate(R.menu.menu_test, menu);

    return true;

}

R.菜单。menu_test是菜单文件的名称。


最后要接收菜单选项的点击,您应该重写以下函数:


@Override

public boolean onOptionsItemSelected(MenuItem item) {

    // Handle action bar item clicks here. The action bar will

    // automatically handle clicks on the Home/Up button, so long

    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();



    if (id == R.id.menu_action_ok) {

        //Your code

        return true;

    }

    return super.onOptionsItemSelected(item);

}

现在你应该有一个像这样的菜单:

//img1.sycdn.imooc.com//6409483100011be406240765.jpg

查看完整回答
反对 回复 2023-03-09
?
四季花海

TA贡献1811条经验 获得超5个赞

您必须为此创建一个菜单。假设你创建add_menu


<?xml version="1.0" encoding="utf-8"?>

<menu

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto">


    <item

        android:id="@+id/action_add"

        android:title="ADD"

        app:showAsAction="ifRoom"/>


</menu>

创建菜单后,使用以下命令将其扩展到您的子活动。


   @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.add_menu, menu);

        return true;

    }


查看完整回答
反对 回复 2023-03-09
?
幕布斯7119047

TA贡献1794条经验 获得超8个赞

您不必使用操作栏。您只能使用您想要的背景颜色的视图。在您将按钮设置在彩色区域上方之后。


在 Android Studio 中:


价值观>风格


用这个 :


<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

代替


 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

回到你的布局:


// Change your background with LinearLayout's background propoerty

   <LinearLayout

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:orientation="horizontal"


       android:background="#FFA908"



       >



       // You can put here whatever you want. İf you wish image or button.




       <Button

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="Example"

           android:layout_gravity="center"

           android:layout_marginLeft="200dp"

           />


       <Button

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="Example"

           android:layout_gravity="center"

           />




   </LinearLayout>

İf 你可以像这样使用。您不需要使用操作栏。你可以轻松地创造任何你想要的东西。


查看完整回答
反对 回复 2023-03-09
  • 3 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信