簡體   English   中英

Android:自定義操作欄,如何使用整個寬度?

[英]Android: Custom action bar,How to use entire width?

這是我的自定義操作欄的圖像:

在此輸入圖像描述

我想使用整個寬度,我已經在互聯網上尋找解決方案但我沒有任何幫助。

這是我的動作欄.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> 

同樣的結果,動作欄右側還有一個空格!

謝謝

閱讀此link1link2並將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視圖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);

這肯定會解決你的目的。 快樂的編碼!!!

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

我通過使用工具欄並為此添加以下屬性解決了這個問題。

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

看看它是否對你有所幫助

我只是想告訴大家,以下有這個代碼的方法不起作用。 屏幕邊緣和工具欄之間的空格仍然在我的情況下。

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

我花了大約2個小時對樣式和這兩個屬性進行一些操作。 什么都沒有幫助我。 我突然想到這個“空間”是否只是工具欄的填充或邊距。 但我的工具欄沒有任何填充或邊距。 我在我的布局.xml文件中添加了0dp填充到Toolbar

android:padding="0dp"

有效。 你可以想象我的臉。 這個空間消失了。 Android欺騙了我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM