Java 从主题向预先存在的工具栏添加按钮
到目前为止,我似乎需要提供一个自定义工具栏来添加任何按钮。从我微薄的理解,我只能得到“3点”显示在工具栏的右侧的主题。除此之外,“3点”按钮似乎只允许菜单项 以下代码导致崩溃:Java 从主题向预先存在的工具栏添加按钮,java,android,android-toolbar,Java,Android,Android Toolbar,到目前为止,我似乎需要提供一个自定义工具栏来添加任何按钮。从我微薄的理解,我只能得到“3点”显示在工具栏的右侧的主题。除此之外,“3点”按钮似乎只允许菜单项 以下代码导致崩溃: toolbar = findViewById( R.id.notes_toolbar ); setSupportActionBar( toolbar ); 因此,我得到: Caused by: java.lang.IllegalStateException: This Activity already has an a
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.
避免重写几个类文件中的几个,有没有简单的方法解决这个问题
谢谢你
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>
活动\课程\笔记.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>
似乎我需要提供一个自定义工具栏来添加任何按钮
您不需要自定义工具栏
来添加在操作栏中显示为按钮的操作栏项。当然,欢迎您使用工具栏
,但这不是必需的
从我微薄的理解,我只能得到“3点”显示在工具栏的右侧的主题
或者,您可以对菜单资源进行充气,并添加显示在溢出(“3点”)或按钮(如果有空间)中的操作栏项。这是将内容添加到操作栏的经典方法,您也可以使用工具栏
来完成
有没有简单的方法来解决这个问题
在崩溃方面,不要使用带有动作栏的主题。例如,您可以将
parent=“Theme.AppCompat.Light.darkaActionBar”
替换为parent=“Theme.AppCompat.Light.NoActionBar”
,这是一个非常简单的解释,我认为我可以轻松地将其应用到其他活动内容中。谢谢
<?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>