You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Paper中Appbar三点菜单显示在左侧而非右侧的问题

Fix: react-native-paper Menu showing on wrong side of Appbar

I see the issue here—your Menu is using the entire Appbar.Header as its anchor instead of the specific three-dot Appbar.Action button. That's why it's aligning to the left side of the header instead of the right, where your menu trigger lives.

Here's how to fix this:

Step 1: Attach the Menu directly to the trigger button

Move the Menu component inside the Appbar.Header and set its anchor to the three-dot action button. This ensures the menu positions itself relative to the exact element that triggers it.

Updated Code

import React, { useEffect, useState } from 'react'
import { SafeAreaView, StyleSheet, Platform } from 'react-native'
import { connect } from 'react-redux'
...
import { createMaterialTopTabNavigator } from '@react-navigation/material-top-tabs';
import { Appbar, Menu } from 'react-native-paper'
import MComponent from './subpages/component'
const Tab = createMaterialTopTabNavigator();
const TAG = 'TAG'

const MPage = ({ navigation, handleLoadData, isLoading }) => {
  useEffect(() => {
    handleLoadData()
  }, [])
  const [openMenu, setOpenMenu] = useState(false)

  return (
    <SafeAreaView style={styles.container}>
      <Appbar.Header statusBarHeight={0}>
        <Appbar.BackAction onPress={() => navigation.goBack()} />
        <Appbar.Content title="App Screen" />
        {/* Menu now anchors directly to the three-dot action button */}
        <Menu
          visible={openMenu}
          onDismiss={() => setOpenMenu(false)}
          anchor={
            <Appbar.Action 
              icon="dots-vertical" 
              onPress={() => setOpenMenu(true)} 
            />
          }
        >
          <Menu.Item 
            onPress={() => {
              setOpenMenu(false)
              handleLoadServiceOrders()
            }} 
            title="Option 1" 
          />
          <Menu.Item onPress={() => {}} title="Option 2" />
        </Menu>
      </Appbar.Header>

      <Loading loading={isLoading} />
      <Tab.Navigator>
        <Tab.Screen name={OPTIONS.PAGE_A} component={MComponent} />
        <Tab.Screen name={OPTIONS.PAGE_B} component={MComponent} />
      </Tab.Navigator>
    </SafeAreaView>
  )
}

const styles = StyleSheet.create({
  container: {
    marginTop: Platform.OS === 'android' ? 30 : 0,
    justifyContent: 'center',
    flex: 1
  }
})

Why this works

By targeting the Appbar.Action as the anchor instead of the whole header, react-native-paper's Menu component calculates its position relative to that specific button. This guarantees it appears on the right side (or directly adjacent to the trigger, depending on platform and screen space) as you expect.

If you still notice minor alignment tweaks are needed, you can adjust the Menu's style with something like style={{ marginRight: 8 }} to fine-tune its position.

内容的提问来源于stack exchange,提问作者Marcos Paulo Júnior

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 10:38:12