Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.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 Actionbar - Fatal编程技术网

Android:自定义操作栏,如何使用整个宽度?

Android:自定义操作栏,如何使用整个宽度?,android,android-actionbar,Android,Android Actionbar,以下是我的自定义操作栏的图像: 我想使用整个宽度,我已经在互联网上搜索了一个解决方案,但没有找到适合我的解决方案 这是我的action bar.xml: 我通过使用工具栏并向其添加以下属性来解决此问题 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width

以下是我的自定义操作栏的图像:

我想使用整个宽度,我已经在互联网上搜索了一个解决方案,但没有找到适合我的解决方案

这是我的action bar.xml:


我通过使用工具栏并向其添加以下属性来解决此问题

<FrameLayout 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="wrap_content" >

<View
    android:id="@+id/toolbar_blurrbg"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/toolbar_bg"
    android:visibility="gone" />

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/actionbar_bg"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp" />

应用程序:ContentInsett和应用程序:ContentInsettStart

查看它是否有助于您阅读此内容,并将contentInsets设置为0dp,如下所示:

View mCustomView = mInflater.inflate(R.layout.actionbar_invoice, null);
getSupportActionBar().setCustomView(mCustomView);
Toolbar toolbar=(Toolbar)mCustomView.getParent();
toolbar.setContentInsetsAbsolute(0,0);
如果使用自定义工具栏而不是ActionBar,请使用以下代码:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="@dimen/action_bar_height"
    android:background="?attr/colorPrimary"
    android:contentInsetStart="0dp"//see this
    android:contentInsetLeft="0dp"//see this
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

我希望这可能会对您有所帮助。

遵循几个简单的步骤。 1.创建自己的自定义操作视图 action_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" 
android:weightSum="3">

<TextView 
    android:id="@+id/bar_title1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="@color/White"
    android:text="title1"/>
<TextView 
    android:id="@+id/bar_title2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="@color/White"
    android:text="title2"/>
<TextView 
    android:id="@+id/bar_title3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:textColor="@color/White"
    android:text="title3"/>
    </LinearLayout>
这肯定会解决你的目的。
快乐编码

我只是想告诉大家,在某种程度上,下面的代码是行不通的。屏幕边缘和工具栏之间的空间仍保留在我的案例中

 ActionBar action = getSupportActionBar();
        action.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

 Toolbar toolbar=(Toolbar)action.getCustomView().getParent();
        toolbar.setContentInsetsAbsolute(0, 0);
        toolbar.getContentInsetEnd();
        toolbar.setPadding(0, 0, 0, 0);
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
我花了大约2个小时对样式和这两个属性进行了一些操作。什么都帮不了我。突然,我想这个“空间”可能只是工具栏的一个填充或边距。但我的工具栏没有任何填充或空白。我在layout.xml文件的
工具栏中添加了
0dp
填充

android:padding="0dp"

成功了。你可以想象我的脸。这个空间已经消失了。安卓骗了我。

BOOOOOM,Thaaaaan,你真是个笨蛋,真管用!!:-)我真的很感谢你的帮助,再次感谢你我应该把这个工具栏放在哪里,这样它就可以在我的项目上添加阴影了?!嘿@Amrut Bidri!我在安卓M上遇到了同样的问题,似乎“SetContentInsertSabsolute(0,0)”的解决方法只适用于Lolipop,但它现在适用于安卓M:(你知道吗?除了回答:我正在使用ActionBar(不支持ActionBar),它只需添加以下代码就可以了:
Toolbar parent=(Toolbar)mCustomAB.getParent();parent.setContentInsetsAbsolute(0,0);
你搞定了。谢谢:)actionBar.setDisplayOptions(actionBar.DISPLAY_-SHOW_-CUSTOM);这个代码片段做了一个小动作。为了尝试这一个…actionBar.setDisplayOptions(actionBar.DISPLAY_-SHOW_-CUSTOM)。谢谢你!Toolbar.setPadding(0,0,0,0);为我工作。谢谢
android:padding="0dp"