android - 工具栏小部件未占用全宽

android - ToolBar widget did not take full width

我正在学习 android,我是新手, 并对 ToolBar 感到困惑,一直在阅读其他所有内容,但似乎对我不起作用。 在 MainActivity 中,工具栏工作正常,工具栏中的按钮向右移动而没有填充 但是当我搬到另一个 activity 按钮没有放在右边,好像那里有填充,

这是我的 toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
>
   <android.support.v7.widget.Toolbar
       android:id="@+id/toolbar"
       android:layout_width="match_parent"
       android:layout_height="?attr/actionBarSize"
       android:background="#5f0e92"
       app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>

我的第二个activity

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="@layout/toolbar"
    android:id="@+id/include"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />
<ScrollView
    android:id="@+id/scrollOff"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:layout_above="@+id/TotShow"
    android:layout_below="@+id/include"
    android:visibility="gone">
    <TableLayout
        android:layout_width="match_parent" android:layout_height="32dp"
        android:gravity="center_horizontal"
        android:id="@+id/tableLayoutOff"
        android:stretchColumns="*">
    </TableLayout>
</ScrollView>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Small Text"
    android:id="@+id/TotShow"
    android:layout_gravity="center_horizontal"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

onCreate 函数我把这个

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

------------编辑----------------

忘了 button/toggle toggle.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:gravity="right">
<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:background="@android:drawable/ic_menu_help"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/actionbar_service_toggle"
    android:layout_toStartOf="@+id/actionbar_service_toggle" />
<ToggleButton
    android:id="@+id/actionbar_service_toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOff="inchi"
    android:textOn="milli"
    android:gravity="right"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

   </RelativeLayout>

自定义视图:

    ActionBar actionBar = getSupportActionBar();
    actionBar.setCustomView(R.layout.togle);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);

在我的 MainActivity 中,这项工作非常好

秒 activity 似乎有正确的填充

我解决了这个问题 很抱歉给您带来麻烦 看来我在我的代码上做了一些愚蠢的事情, 我不小心保留了这段代码:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present..

    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

好吧,目前我正在了解这个 android,所以我只是从主 activity 复制粘贴代码,然后在主 activity 中禁用添加菜单后,我忘了在第二个 activity 中禁用它,所以当我尝试单击它时,右侧有一个透明的菜单,按钮起作用了,这就是我意识到错误的地方。