安卓:自定义操作栏,如何使用整个宽度? [英] Android: Custom action bar,How to use entire width?

查看:238
本文介绍了安卓:自定义操作栏,如何使用整个宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

下面是我的操作栏中的.xml:

< XML版本=1.0编码=UTF-8? > < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android     机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT>     < ImageView的         机器人:layout_width =50dp         机器人:layout_height =match_parent         机器人:ID =@ + ID / invActionBarSave         机器人:SRC =@可绘制/ ic_ok_b         机器人:nestedScrollingEnabled =真         机器人:layout_alignParentRight =真         机器人:后台=#ffb0afaf         机器人:以下属性来=8DP         机器人:paddingRight =8DP         机器人:layout_marginTop =4DP         机器人:layout_marginBottom =4DP/>     < ImageView的         机器人:layout_width =50dp         机器人:layout_height =match_parent         机器人:ID =@ + ID / invActionBarDelete         机器人:SRC =@可绘制/ ic_trash         机器人:nestedScrollingEnabled =真         机器人:layout_toLeftOf =@ + ID / invActionBarSave         机器人:layout_marginRight =5DP         机器人:后台=#ffb0afaf         机器人:以下属性来=8DP         机器人:paddingRight =8DP         机器人:layout_marginTop =4DP         机器人:layout_marginBottom =4DP/>     <的TextView         机器人:layout_width =WRAP_CONTENT         机器人:layout_height =match_parent         机器人:单线=真         机器人:文本=אלסאדקאחמדאבוהאני         机器人:TEXTSIZE =18dp         机器人:文字颜色=#FF000000         机器人:ID =@ + ID / invActionBarTitle         机器人:重力=中心|右         机器人:layout_toLeftOf =@ + ID / invActionBarDelete         机器人:layout_toRightOf =@ + ID / invActionBarHome         机器人:layout_marginRight =20dp         机器人:layout_marginLeft =10dp/>     < ImageView的         机器人:layout_width =WRAP_CONTENT         机器人:layout_height =match_parent         机器人:ID =@ + ID / invActionBarHome         机器人:SRC =@可绘制/ ic_back_b         机器人:nestedScrollingEnabled =真         机器人:layout_alignParentLeft =真         机器人:layout_alignParentRight =假         机器人:以下属性来=10dp/>     <进度         机器人:layout_width =match_parent         机器人:layout_height =WRAP_CONTENT         机器人:indeterminateOnly =假         风格=@风格/ ProgressBar.Horizo​​ntal         机器人:不确定=真         机器人:ID =@ + ID / invActionBarProgressBar         机器人:能见度=水涨船高         机器人:layout_below =@ + ID / invActionBarDelete         机器人:以下属性来=10dp/> < / RelativeLayout的>

这里是我的Java code:

  getSupportActionBar()setDisplayShowHomeEnabled(假)。
    getSupportActionBar()setDisplayShowTitleEnabled(假)。
    LayoutInflater mInflater = LayoutInflater.from(本);
    //查看mCustomView = mInflater.inflate(R.layout.actionbar_invoice,NULL);
    getSupportActionBar()setCustomView(R.layout.actionbar_invoice)。
    getSupportActionBar()setDisplayShowCustomEnabled(真)。
    的setContentView(R.layout.cstmrinvoice);
    getWindow()。
            getDecorView()。
            setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
 

下面是一个简单的:

这里是的.xml:

< XML版本=1.0编码=UTF-8? > < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android     机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT     机器人:contentInsetEnd =0dp     机器人:contentInsetStart =0dp>     <的TextView         机器人:layout_width =match_parent         机器人:layout_height =match_parent         机器人:textAppearance =机器人:ATTR / textAppearanceSmall         机器人:文本=小文         机器人:ID =@ + ID / textView6         机器人:后台=#FFFF0000         机器人:文字颜色=#FFFFFFFF/> < / RelativeLayout的>

同样的结果,仍然在操作栏右侧的空间!

感谢您

解决方案

阅读<一个href="http://developer.android.com/reference/android/widget/Toolbar.html#setContentInsetsAbsolute(int,%20int)"相对=nofollow>链接1 ,链接2 并设置contentInsets为0dp象下面这样做的:

 查看mCustomView = mInflater.inflate(R.layout.actionbar_invoice,NULL);
getSupportActionBar()setCustomView(mCustomView)。
工具条工具栏=(栏)mCustomView.getParent();
toolbar.setContentInsetsAbsolute(0,0);
 

如果您使用的是自定义工具栏,而不是动作条然后用低于code:

 &LT; android.support.v7.widget.Toolbar
    机器人:ID =@ + ID /工具栏
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =match_parent
    机器人:=了minHeight@扪/ action_bar_height
    机器人:ATTR / colorPrimary后台=
    机器人:contentInsetStart =0dp//看到这
    机器人:contentInsetLeft =0dp//看到这
    应用程序:contentInsetLeft =0dp
    应用程序:contentInsetStart =0dp
    应用程序:主题=@风格/ ThemeOverlay.AppCompat.Dark.ActionBar
    应用程序:popupTheme =@风格/ ThemeOverlay.AppCompat.Light&GT;
 

我希望这可以帮助你。

Here is an image of my custom action bar:

I want to use the entire width, I have search the internet for a solution but nothing wotrked for me.

Here is my action bar .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>

And here is my java code:

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

[EDIT]

Here is a simple one:

And here is the .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>

The same result, there still a space at right of the action bar !

Thank you

解决方案

Read this link1, link2 and set the contentInsets to 0dp like done below:

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

If you are using custom Toolbar instead of ActionBar then use below code:

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

I Hope this might help you.

这篇关于安卓:自定义操作栏,如何使用整个宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆