在Android Studio中的选项菜单在标签式活动中?

huangapple go评论72阅读模式
英文:

Option menu in a tabbed activity in android studio?

问题

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.activity_menu, menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    if(item.getItemId() == R.id.about){
        //do something
        return true;
    }
    return super.onOptionsItemSelected(item);
}
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/about"
        app:showAsAction="never"
        android:title="Despre"/>
</menu>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ubbscraper">
    <!-- Permissions and other configurations -->
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme1">
        <activity android:name=".AboutActivity"
            android:parentActivityName=".AddSubjectsActivity"/>
        <activity
            android:name=".AddSubjectsActivity"
            android:launchMode="singleTop"
            android:parentActivityName=".MainActivity" />
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme1"
            android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>
<androidx.coordinatorlayout.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"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minHeight="?actionBarSize"
            android:padding="@dimen/appbar_padding"
            android:text="@string/app_name"
            android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary" />
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_input_add"
        android:tint="@android:color/white"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

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

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

    <style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch(item.getItemId()){
        case R.id.about:
            return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_menu, menu);
    return true;
}
英文:

I am trying to create a fragmented activity with 2 fragments in android studio with an options menu with item as showAsAction:never. I have tried everything and have overridden onOptionsItemSelected and onCreateOptionsMenu in both fragments and setHasOptionsMenu(true). After that i tried to change the theme of my activity from NoActionBar to something else but every theme i tried did not work.

Here's some of my code on fragments:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.activity_menu, menu);

    }


    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId() == R.id.about){
            //do something
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

...and there is my manifest:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.ubbscraper&quot;&gt;

    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot; /&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:networkSecurityConfig=&quot;@xml/network_security_config&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme1&quot;&gt;
        &lt;activity android:name=&quot;.AboutActivity&quot;
            android:parentActivityName=&quot;.AddSubjectsActivity&quot;/&gt;
        &lt;activity
            android:name=&quot;.AddSubjectsActivity&quot;
            android:launchMode=&quot;singleTop&quot;
            android:parentActivityName=&quot;.MainActivity&quot; /&gt;
        &lt;activity
            android:name=&quot;.MainActivity&quot;
            android:label=&quot;@string/app_name&quot;
            android:theme=&quot;@style/AppTheme1&quot;
            android:launchMode=&quot;singleTop&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&lt;/manifest&gt;

The theme that i am trying to change is in the MainActivity.
Here is the menu code:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
    &lt;item
        android:id=&quot;@+id/about&quot;
        app:showAsAction=&quot;never&quot;
        android:title=&quot;Despre&quot;/&gt;
&lt;/menu&gt;

My activity_main.xml:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.MainActivity&quot;&gt;

    &lt;com.google.android.material.appbar.AppBarLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:theme=&quot;@style/AppTheme.AppBarOverlay&quot;&gt;

        &lt;TextView
            android:id=&quot;@+id/title&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:gravity=&quot;center&quot;
            android:minHeight=&quot;?actionBarSize&quot;
            android:padding=&quot;@dimen/appbar_padding&quot;
            android:text=&quot;@string/app_name&quot;
            android:textAppearance=&quot;@style/TextAppearance.Widget.AppCompat.Toolbar.Title&quot; /&gt;

        &lt;com.google.android.material.tabs.TabLayout
            android:id=&quot;@+id/tabs&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:background=&quot;?attr/colorPrimary&quot; /&gt;
    &lt;/com.google.android.material.appbar.AppBarLayout&gt;

    &lt;androidx.viewpager.widget.ViewPager
        android:id=&quot;@+id/view_pager&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;
        app:layout_behavior=&quot;@string/appbar_scrolling_view_behavior&quot; /&gt;

    &lt;com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id=&quot;@+id/fab&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_gravity=&quot;bottom|end&quot;
        android:layout_margin=&quot;@dimen/fab_margin&quot;
        app:srcCompat=&quot;@android:drawable/ic_input_add&quot;
        android:tint=&quot;@android:color/white&quot;/&gt;
&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

style.xml:

&lt;resources&gt;
    &lt;!-- Base application theme. --&gt;
    &lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.AppCompat.Light.NoActionBar&quot;&gt;
        &lt;!-- Customize your theme here. --&gt;
        &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorPrimary&lt;/item&gt;
        &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;
        &lt;item name=&quot;colorAccent&quot;&gt;@color/colorAccent&lt;/item&gt;
    &lt;/style&gt;

    &lt;style name=&quot;AppTheme.NoActionBar&quot;&gt;
        &lt;item name=&quot;windowActionBar&quot;&gt;false&lt;/item&gt;
        &lt;item name=&quot;windowNoTitle&quot;&gt;true&lt;/item&gt;
    &lt;/style&gt;

    &lt;style name=&quot;AppTheme.AppBarOverlay&quot; parent=&quot;ThemeOverlay.AppCompat.Dark.ActionBar&quot; /&gt;

    &lt;style name=&quot;AppTheme.PopupOverlay&quot; parent=&quot;ThemeOverlay.AppCompat.Light&quot; /&gt;

    &lt;style name=&quot;AppTheme1&quot; parent=&quot;Theme.AppCompat.Light.NoActionBar&quot;&gt;
        &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorPrimary&lt;/item&gt;
        &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;
        &lt;item name=&quot;colorAccent&quot;&gt;@color/colorAccent&lt;/item&gt;
        &lt;item name=&quot;windowActionBar&quot;&gt;false&lt;/item&gt;
        &lt;item name=&quot;android:windowNoTitle&quot;&gt;true&lt;/item&gt;
    &lt;/style&gt;

&lt;/resources&gt;

MainActivity.java:

@Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()){
            case R.id.about:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_menu, menu);
        return true;
    }

EDIT

I edited the code and the app name is shown only once but the menu isn't shown

The result

答案1

得分: 0

**将以下内容添加到你的style.xml文件中:**

     <style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar">
          <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
    </style>


**然后在清单文件中添加apptheme1:**

     <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network_security_config"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme1">


对于三个点(...):
你必须在MainActivity的布局中将工具栏(toolbar)放在AppBarLayout内,
然后在MainActivity的onCreate中添加`setSupportActionBar(toolbar)`
英文:

add this your style.xml:

 &lt;style name=&quot;AppTheme1&quot; parent=&quot;Theme.AppCompat.Light.NoActionBar&quot;&gt;
      &lt;item name=&quot;colorPrimary&quot;&gt;@color/colorPrimary&lt;/item&gt;
    &lt;item name=&quot;colorPrimaryDark&quot;&gt;@color/colorPrimaryDark&lt;/item&gt;
    &lt;item name=&quot;colorAccent&quot;&gt;@color/colorAccent&lt;/item&gt;
    &lt;item name=&quot;windowActionBar&quot;&gt;false&lt;/item&gt;
    &lt;item name=&quot;android:windowNoTitle&quot;&gt;true&lt;/item&gt;
&lt;/style&gt;

then add apptheme1 in manifest:-

 &lt;application
    android:allowBackup=&quot;true&quot;
    android:icon=&quot;@mipmap/ic_launcher&quot;
    android:label=&quot;@string/app_name&quot;
    android:networkSecurityConfig=&quot;@xml/network_security_config&quot;
    android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
    android:supportsRtl=&quot;true&quot;
    android:theme=&quot;@style/AppTheme1&quot;&gt;

For 3 dots :
You must include toolbar inside appbarlayout your layout of Mainactivity
Then add setSupportActionBar(toolbar) on oncreate of Mainactivity

huangapple
  • 本文由 发表于 2020年9月7日 20:12:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63777397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定