【问题标题】:android - ToolBar widget did not take full widthandroid - 工具栏小部件没有占用全宽
【发布时间】:2016-09-20 12:59:38
【问题描述】:

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

这是我的工具栏.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>

我的第二个活动

 <?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);

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

忘记了按钮/切换 切换.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 中,这项工作非常好

在第二个活动中似乎有正确的填充

【问题讨论】:

  • 您可以发布与您的按钮相关的任何内容吗?代码你怎么把它加到工具栏上?

标签: java android android-toolbar


【解决方案1】:

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

@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,所以我只是从主活动中复制粘贴代码,在主活动中禁用添加菜单后,我忘记在第二个活动中禁用它,所以有一个菜单当我尝试单击它时透明的右侧,按钮起作用,这就是我意识到我的错误的地方。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2016-08-23
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多