从主题中将按钮添加到预先存在的工具栏

Adding a Button to a Pre-existing ToolBar from the Theme

到目前为止,我似乎需要提供自定义工具栏才能添加任何按钮。根据我的微薄理解,我只能在主题给出的工具栏右侧显示“3个点”。最重要的是,“3 点”按钮似乎只允许一个项目菜单。

以下代码导致崩溃:

toolbar = findViewById( R.id.notes_toolbar );
setSupportActionBar( toolbar );

因此,我得到:

Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

避免重写几个 class 文件,有解决这个问题的简单方法吗?

谢谢你。

styles.xml :

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>

Course_NoteTaking_Activity.java :

public class Course_NoteTaking_Activity extends AppCompatActivity {

    private Button saveButton;

    private EditText notes_EditText;

    private Toolbar toolbar;


    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_course_notetaking );




        toolbar = findViewById( R.id.notes_toolbar );





        //TODO  CAUSES A CRASH FROM PRE-EXISTING TOOLBAR.
        //setSupportActionBar( toolbar );





        //getSupportActionBar().setDisplayHomeAsUpEnabled( false );
        //getSupportActionBar().setTitle( "Notes for the Course" );


        Intent intent = getIntent();
        String testString = intent.getStringExtra( "notes" );


        saveButton = findViewById( R.id.notes_saveButtonXML );
        saveButton.setOnClickListener( new View.OnClickListener() {

            @Override
            public void onClick( View view ) {

                Intent returnIntent = new Intent();

                String transferString = "Just testing... 1, 2, and a 3!!!";

                returnIntent.putExtra( "return_Notes", transferString );

            }
        });


        notes_EditText = findViewById( R.id.notes_EditTextXML );
        notes_EditText.setText( testString );

    }

}

activity_course_notetaking.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="wrap_content"
    android:orientation="vertical"
    >


    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/notes_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:elevation="4dp"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="8dp"
            android:text="Test Button"
            android:layout_gravity="right"
            />

    </android.support.v7.widget.Toolbar>


    <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:gravity="center_horizontal"
        android:orientation="vertical"

        android:scrollbarAlwaysDrawVerticalTrack="true"
        android:scrollbars="vertical"
        android:showDividers="none"

        tools:context="com.weslange.Term_Scheduling.Course_ChangingDetails_Activity"
        >


        <Button
            android:id="@+id/notes_saveButtonXML"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Return to the Course's Details (Not Saved Yet)"

            android:layout_marginVertical="2dp"
            />


        <EditText
            android:id="@+id/notes_EditTextXML"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPersonName|textCapWords"

            android:imeOptions="actionDone"

            android:layout_marginVertical="2dp"
            />

    </LinearLayout>

</RelativeLayout>

it seems that I need to provide a custom Toolbar in order to add any Button

您不需要自定义 Toolbar 来添加在操作栏中显示为按钮的操作栏项目。当然,欢迎您使用 Toolbar,但这不是必需的。

From my meager understanding, I can only get "3 dots" to show in the right-side of the Toolbar given by the Theme

或者,您可以扩充菜单资源,并添加显示在溢出(“3 个点”)或按钮(如果有空间)中的操作栏项目。这是向操作栏添加内容的经典方法,您也可以使用 Toolbar 来完成。

is there a simple way of addressing this?

关于崩溃,不要使用带有操作栏的主题。例如,您可以将 parent="Theme.AppCompat.Light.DarkActionBar" 替换为 parent="Theme.AppCompat.Light.NoActionBar"