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

12

这里是我的自定义操作栏的图片:

在此输入图片描述

我想使用整个宽度,但我在网上搜索了解决方案,但没有一个对我起作用。

这是我的操作栏 .xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:id="@+id/invActionBarSave"
        android:src="@drawable/ic_ok_b"
        android:nestedScrollingEnabled="true"
        android:layout_alignParentRight="true"
        android:background="#ffb0afaf"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:id="@+id/invActionBarDelete"
        android:src="@drawable/ic_trash"
        android:nestedScrollingEnabled="true"
        android:layout_toLeftOf="@+id/invActionBarSave"
        android:layout_marginRight="5dp"
        android:background="#ffb0afaf"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="4dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:singleLine="true"
        android:text="אלסאדק אחמד אבו האני"
        android:textSize="18dp"
        android:textColor="#ff000000"
        android:id="@+id/invActionBarTitle"
        android:gravity="center|right"
        android:layout_toLeftOf="@+id/invActionBarDelete"
        android:layout_toRightOf="@+id/invActionBarHome"
        android:layout_marginRight="20dp"
        android:layout_marginLeft="10dp" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/invActionBarHome"
        android:src="@drawable/ic_back_b"
        android:nestedScrollingEnabled="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="false"
        android:paddingLeft="10dp" />

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminateOnly="false"
        style="@style/ProgressBar.Horizontal"
        android:indeterminate="true"
        android:id="@+id/invActionBarProgressBar"
        android:visibility="gone"
        android:layout_below="@+id/invActionBarDelete"
        android:paddingLeft="10dp" />

</RelativeLayout>

这是我的Java代码:

getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    LayoutInflater mInflater = LayoutInflater.from(this);
    //View mCustomView = mInflater.inflate(R.layout.actionbar_invoice, null);
    getSupportActionBar().setCustomView(R.layout.actionbar_invoice);
    getSupportActionBar().setDisplayShowCustomEnabled(true);
    setContentView(R.layout.cstmrinvoice);
    getWindow().
            getDecorView().
            setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

[编辑]

以下是一个简单的示例:

输入图像说明

这是 .xml 文件的内容:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:contentInsetEnd="0dp"
    android:contentInsetStart="0dp">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/textView6"
        android:background="#ffff0000"
        android:textColor="#ffffffff" />
</RelativeLayout>

相同的结果,仍然在操作栏右侧有一个空间!

谢谢

5个回答

26

阅读此链接1链接2并像下面所示将contentInsets设置为0dp:

View mCustomView = mInflater.inflate(R.layout.actionbar_invoice, null);
getSupportActionBar().setCustomView(mCustomView);
Toolbar toolbar=(Toolbar)mCustomView.getParent();
toolbar.setContentInsetsAbsolute(0,0);

如果您使用自定义工具栏而不是操作栏,请使用以下代码:

<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">

我希望这可以帮助到您。


2
非常感谢,太棒了,它起作用了!! :-) 我真的很感激你的帮助,再次感谢。 - user2005049
我应该将这个工具栏放在哪里,以便它可以在我的项目上添加阴影?! - Mahdi
嘿@Amrut Bidri!我在Android M上遇到了同样的问题,似乎使用“setContentInsetsAbsolute(0,0)”的解决方法仅适用于Lolipop,但它现在不适用于Android M :( 你知道这方面的情况吗? - Samik
回答中添加:我正在使用ActionBar(而不是支持ActionBar),只需添加此代码即可正常工作: Toolbar parent =(Toolbar)mCustomAB.getParent(); parent.setContentInsetsAbsolute(0,0); - Mert Gülsoy

8

请按照以下简单步骤操作: 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>

以下是代码:

 final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.custom_action_bar, null);
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

这一定能解决你的问题。 编程愉快!!!

1
你做得很好。谢谢 :) actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 这段代码片段解决了问题。 - N-JOY
我浪费了几个小时来尝试这个... actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)。谢谢! - wtk

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

toolbar.setPadding(0, 0, 0, 0); 对我很有效。谢谢。 - Vikasdeep Singh

0

我通过使用工具栏并将以下属性添加到其中来解决了这个问题。

<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" />

app:contentInsetEnd 和 app:contentInsetStart..

看看它是否有帮助


0

我只是想告诉大家,以下代码在某种程度上不起作用。在我的情况下,屏幕边缘和工具栏之间保留了一个空格。

app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"

我花了大约2个小时来处理样式和这两个属性。但是没有什么帮助。突然间,我想到这个“空格”可能只是工具栏的填充或边距。但是我的工具栏没有任何填充或边距。于是我在我的布局.xml文件中为工具栏添加了0dp填充。
android:padding="0dp"

它奏效了。你可以想象我的表情。这个空间已经消失了。Android欺骗了我。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接