Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android工具栏上奇怪的空白_Android_Android Toolbar - Fatal编程技术网

Android工具栏上奇怪的空白

Android工具栏上奇怪的空白,android,android-toolbar,Android,Android Toolbar,我正在把工具栏放到屏幕底部。隐藏工具栏标题后,标题仍占据空间 我的问题是:如何删除该空间 styles.xml <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. -->

我正在把
工具栏
放到屏幕底部。隐藏工具栏标题后,标题仍占据空间

我的问题是:如何删除该空间


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>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
</resources>
在同一活动的
oncreateoptions菜单中
onoptions项选中

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return super.onOptionsItemSelected(item);
}
在我的布局中:

<?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="match_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".WelcomeActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/lbl_test"
            android:layout_width="match_parent"
            android:layout_height="20sp"
            android:text=""
            />
    </LinearLayout>
    <android.support.v7.widget.Toolbar
        android:id="@+id/bar_bottom"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary"
        android:layout_alignParentBottom="true"
         />
</RelativeLayout>

和菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:context=".WelcomeActivity">
    <item
        android:id="@+id/btn_1"
        android:orderInCategory="100"
        android:title="Button 1"
        app:showAsAction="always" />
    <item
        android:id="@+id/btn_2"
        android:orderInCategory="200"
        android:title="Button 2"
        app:showAsAction="always" />
    <item
        android:id="@+id/btn_3"
        android:orderInCategory="300"
        android:title="Button 3"
        app:showAsAction="always" />
    <item
        android:id="@+id/btn_4"
        android:orderInCategory="400"
        android:title="Button 4"
        app:showAsAction="always" />
</menu>

据我所知,一旦将工具栏设置为SupportActionBar,即使将其设置为空字符串,它也会保留标题右侧的空间

此外,当您删除标题并且没有图标或导航时,实际上您对ActionBar不感兴趣,而只对工具栏感兴趣。为什么不按下面的方式扩展工具栏并使用它,以便通过膨胀菜单添加到工具栏中的项目的间距相等

public class SplitToolbar extends Toolbar {

    public SplitToolbar(Context context) {
        super(context);
    }

    public SplitToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SplitToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        if (child instanceof ActionMenuView) {
            params.width = LayoutParams.MATCH_PARENT;
        }
        super.addView(child, params);
    }
}
如下实例化并膨胀XML

SplitToolbar toolbar = (SplitToolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.inflateMenu(R.menu.menu_toolbar);
在布局XML中添加如下工具栏

<com.xxx.xxx.xxx.SplitToolbar
                android:id="@+id/my_awesome_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/primary" />

最后是相同的菜单XML

<menu 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"
    tools:context=".XXXActivity">

    <item
        android:id="@+id/action_zoom2fit"
        android:icon="@drawable/ic_aspect_ratio_white_24dp"
        android:orderInCategory="100"
        android:title="@string/zoomfit"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_card"
        android:icon="@drawable/ic_action_eject"
        android:orderInCategory="100"
        android:title="@string/showcard"
        app:showAsAction="always" />
    <item
        android:id="@+id/actionZoom2Track"
        android:icon="@drawable/ic_drive_eta_white_24dp"
        android:orderInCategory="100"
        android:title="@string/zoom2Tracked"
        app:showAsAction="always" />

</menu>

注意/免责声明:用于此目的的工具栏扩展实际上源自我在StackOverflow本身上找到的一个答案。我正试图找到相同的人给予信贷。我找到后一定会加进去的。 我相信我是从这条线得到的
也许我还来得及

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.Toolbar
    android:id="@+id/actionbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/Base.Theme.AppCompat.CompactMenu"
    android:layout_alignParentBottom="true">

    <LinearLayout
        android:id="@+id/toolbarMenuLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="3"
        android:gravity="center_horizontal">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="true"
            android:scaleType="fitXY"
            android:src="@drawable/ic_action_location" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="true"
            android:scaleType="fitXY"
            android:src="@drawable/ic_action_refresh" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="true"
            android:scaleType="fitXY"
            android:src="@drawable/ic_action_settings" />

    </LinearLayout>

</android.support.v7.widget.Toolbar>


我没有隐藏工具栏,我只是通过
getSupportActionBar()隐藏工具栏标题。setDisplayShowTitleEnabled(false)
将gravity设置为工具栏,因为android:gravity=“start”菜单项总是从右侧开始。我知道,但我希望这4个菜单项占据相同的全宽。我如何才能做到这一点?@Raptor如果您以这种方式使用工具栏,您将无法按照自己的意愿对齐内容。相反,您可以将自定义布局添加到工具栏,然后自己管理所有项目单击,而不使用settint作为操作栏(这很糟糕,因为您必须对所有侦听器进行编码)。看来你的目标,就像G+底部的横条一样,只有这样才能实现。谢谢,我会尝试一下并回复你。经过测试,部分效果良好。分辨率为720x1280,只能显示3个菜单按钮(但它们的间距相等,很好!)@Raptor很高兴为您提供帮助!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.Toolbar
    android:id="@+id/actionbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/Base.Theme.AppCompat.CompactMenu"
    android:layout_alignParentBottom="true">

    <LinearLayout
        android:id="@+id/toolbarMenuLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="3"
        android:gravity="center_horizontal">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="true"
            android:scaleType="fitXY"
            android:src="@drawable/ic_action_location" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="true"
            android:scaleType="fitXY"
            android:src="@drawable/ic_action_refresh" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="true"
            android:scaleType="fitXY"
            android:src="@drawable/ic_action_settings" />

    </LinearLayout>

</android.support.v7.widget.Toolbar>