从主题向预先存在的工具栏添加按钮



到目前为止,我似乎需要提供一个自定义工具栏才能添加任何按钮。 根据我的微薄理解,我只能让"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.

避免重写几个类文件中的几个,有没有一种简单的方法来解决这个问题?

谢谢你。

样式.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>

似乎我需要提供一个自定义工具栏才能添加任何按钮

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

根据我的微薄理解,我只能在主题给出的工具栏右侧显示"3 个点">

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

有没有简单的方法来解决这个问题?

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

最新更新