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

前端 未结 5 893
梦如初夏
梦如初夏 2020-12-10 04:54

Here is an image of my custom action bar:

\"enter

I want to use the entire wid

相关标签:
5条回答
  • 2020-12-10 05:35

    I just wanted to tell everybody that IN SOME WAY having this code below does not work. A space between the edge of the screen and the Toolbar remains in my case.

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

    I spent ~2hours doing some manipulations with styles and this two attributes. Nothing helped me. All of a sudden I thought whether this "space" may be just a padding or margin of the Toolbar. But my Toolbar hadn't had any padding or margin. And I added 0dp padding to the Toolbar in my layout .xml file.

    android:padding="0dp"
    

    It worked. You can imagine my face. This space has gone away. Android lied to me.

    0 讨论(0)
  • 2020-12-10 05:42

    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.

    0 讨论(0)
  • 2020-12-10 05:42

    I solved this by using toolbar and adding the following properties to this.

    <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 and app:contentInsetStart ..

    See if it helps you

    0 讨论(0)
  • 2020-12-10 05:44

    Follow few simple steps. 1. Make your own custom Action view 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>
    

    Here is the code:

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

    This will surely solve your purpose. Happy Coding !!!

    0 讨论(0)
  • 2020-12-10 05:47
     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);
    
    0 讨论(0)
提交回复
热议问题