【问题标题】:How do I narrow spacing between up/back arrow and title in actionbar?如何缩小操作栏中上/后箭头和标题之间的间距?
【发布时间】:2018-06-26 08:35:34
【问题描述】:

我知道这个问题之前在这里被问过,我已经查看了解决方案。我想缩小箭头和标题之间的空间,下图空间太大。

最流行的解决方案是在我的工具栏中输入app:contentInsetStartWithNavigation="0dp" XML 代码如下:

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:theme="@style/CustomToolbarStyle"
            app:layout_collapseMode="pin"
            app:contentInsetStartWithNavigation="0dp"/>

当我这样做并运行我的应用程序时,我收到错误消息:

Error:(100) No resource identifier found for attribute 'contentInsetStartWithNavigation' in package 'com.packagename'

谷歌搜索该错误导致我尝试解决方案,而不是添加到我的XML

            android:contentInsetStart="0dp"
            android:contentInsetLeft="0dp"
            android:contentInsetRight="0dp"
            android:contentInsetEnd="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetEnd="0dp"

但是当我加载我的应用程序时,箭头和标题之间的间距没有改变,仍然有很大的空间。

我在我的XML 中使用CoordinatorLayout,但不明白为什么会有任何区别。

另外,在我替换时在我的XML

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/lib/com.mypackage.name"

按照这个解决方案:error: No resource identifier found for attribute 'adSize' in package 'com.google.example' main.xml

我仍然得到错误。

这是我的活动的java 代码,位于onCreate

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

//Show the arrow to go back
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);

//show the App title
actionbar.setTitle("Pop");

这是我的活动 xml 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.chris.populisto.NewContact">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="350dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleTextAppearance="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false"

            >

            <LinearLayout
                android:id="@+id/holder"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="9.5"
                android:orientation="vertical"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                app:layout_collapseMode="parallax">


                <TextView
                    android:id="@+id/textViewInvisible"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:background="@drawable/textboxes_for_contact"
                    android:visibility="invisible" />

                <AutoCompleteTextView
                    android:id="@+id/textViewCategory"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/textboxes_for_contact"
                    android:hint="Category" />

                <EditText
                    android:id="@+id/textViewName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/textboxes_for_contact"
                    android:hint="Name" />

                <EditText
                    android:id="@+id/textViewPhone"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/textboxes_for_contact"
                    android:hint="Phone" />

                <AutoCompleteTextView
                    android:id="@+id/textViewAddress"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/textboxes_for_contact"
                    android:hint="Address" />

                <EditText
                    android:id="@+id/textViewComment"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/textboxes_for_contact"
                    android:hint="Comment" />

                <TextView
                    android:id="@+id/textViewInvisible2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/textboxes_for_contact"
                    android:visibility="invisible" />


            </LinearLayout>

            <!--This is for bar pinned to top of screen-->
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:theme="@style/CustomToolbarStyle"
                app:layout_collapseMode="pin"
/>



        </android.support.design.widget.CollapsingToolbarLayout>


     <fragment
         android:name="com.example.chris.populisto.ButtonSharingFragment"
         android:id = "@+id/myFragment"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
        />

    </android.support.design.widget.AppBarLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <!--This is positioned in the toolbar, above-->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"

            >

            <Button
                android:id="@+id/cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="CANCEL" />

            <Button
                android:id="@+id/save"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toLeftOf="@id/cancel"
                android:layout_marginRight="1dp"
                android:text="SAVE" />

        </RelativeLayout>
    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

这是我的 gradle 依赖项:

dependencies {
    //this is the volley library
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    // compile 'com.android.support:appcompat-v7:+'

    compile 'com.android.volley:volley:1.0.0'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.googlecode.libphonenumber:libphonenumber:8.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.google.code.gson:gson:2.8.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:recyclerview-v7:23.0.0-alpha1'
    compile 'com.google.android.gms:play-services-places:15.0.1'
    compile ('com.android.support:support-v4:23.4.0'){
        force = true;
    }


}

【问题讨论】:

    标签: java android android-actionbar


    【解决方案1】:

    您可以将 customView 添加到您的工具栏:

    Toolbar toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar);
    View logo = getLayoutInflater().inflate(R.layout.toolbar_custom_view_layout, null);
    toolbar.addView(logo);
    

    xml toolbar_custom_view_layout 可以是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/back_arrow_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/back_arrow"
            android:layout_gravity="center_vertical"/>
    
        <TextView
            android:id="@+id/title_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/title"
            android:layout_gravity="center_vertical"
            android:marginStart="12dp"/>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      试试这个

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true">
      
          <android.support.design.widget.AppBarLayout
              android:id="@+id/appbar"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:fitsSystemWindows="true"
              android:theme="@style/AppTheme">
      
              <android.support.design.widget.CollapsingToolbarLayout
                  android:id="@+id/collapsing_toolbar"
                  android:layout_width="match_parent"
                  android:layout_height="350dp"
                  android:fitsSystemWindows="true"
                  app:contentScrim="?attr/colorPrimary"
                  app:expandedTitleTextAppearance="@android:color/transparent"
                  app:layout_scrollFlags="scroll|exitUntilCollapsed"
                  app:titleEnabled="false"
      
                  >
      
                  <LinearLayout
                      android:id="@+id/holder"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"
                      android:layout_weight="9.5"
                      android:orientation="vertical"
                      android:paddingLeft="16dp"
                      android:paddingRight="16dp"
                      app:layout_collapseMode="parallax">
      
      
                      <TextView
                          android:id="@+id/textViewInvisible"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_weight="2"
                          android:visibility="invisible" />
      
                      <AutoCompleteTextView
                          android:id="@+id/textViewCategory"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_weight="1"
                          android:hint="Category" />
      
                      <EditText
                          android:id="@+id/textViewName"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_weight="1"
                          android:hint="Name" />
      
                      <EditText
                          android:id="@+id/textViewPhone"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_weight="1"
                          android:hint="Phone" />
      
                      <AutoCompleteTextView
                          android:id="@+id/textViewAddress"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_weight="1"
                          android:hint="Address" />
      
                      <EditText
                          android:id="@+id/textViewComment"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_weight="1"
                          android:hint="Comment" />
      
                      <TextView
                          android:id="@+id/textViewInvisible2"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:layout_weight="1"
                          android:visibility="invisible" />
      
      
                  </LinearLayout>
      
                  <!--This is for bar pinned to top of screen-->
                  <android.support.v7.widget.Toolbar
                      android:id="@+id/toolbar"
                      android:layout_width="match_parent"
                      android:layout_height="?actionBarSize"
                      app:layout_collapseMode="pin"
                      app:contentInsetEnd="0dp"
                      app:contentInsetLeft="0dp"
                      app:contentInsetRight="0dp"
                      app:contentInsetStart="0dp"
                      app:contentInsetStartWithNavigation="0dp"
                      />
      
      
      
              </android.support.design.widget.CollapsingToolbarLayout>
      
      
      
          </android.support.design.widget.AppBarLayout>
      
      
          <android.support.v7.widget.RecyclerView
              android:id="@+id/rv"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:scrollbars="vertical"
              app:layout_behavior="@string/appbar_scrolling_view_behavior" />
      
          <!--This is positioned in the toolbar, above-->
          <LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="50dp"
              android:orientation="vertical">
      
              <RelativeLayout
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"
      
                  >
      
                  <Button
                      android:id="@+id/cancel"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentRight="true"
                      android:text="CANCEL" />
      
                  <Button
                      android:id="@+id/save"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_toLeftOf="@id/cancel"
                      android:layout_marginRight="1dp"
                      android:text="SAVE" />
      
              </RelativeLayout>
          </LinearLayout>
      
      </android.support.design.widget.CoordinatorLayout>
      

      输出使用

      app:contentInsetEnd="0dp"
      app:contentInsetLeft="0dp"
      app:contentInsetRight="0dp"
      app:contentInsetStart="0dp"
      app:contentInsetStartWithNavigation="0dp"
      

      不使用的输出

      app:contentInsetEnd="0dp"
      app:contentInsetLeft="0dp"
      app:contentInsetRight="0dp"
      app:contentInsetStart="0dp"
      app:contentInsetStartWithNavigation="0dp"
      

      编辑

      请更新您的dependencies

      implementation 'com.android.support:appcompat-v7:27.1.1'
      implementation 'com.android.support:support-v4:27.1.1'
      implementation 'com.android.support:design:27.1.1'
      

      【讨论】:

      • 谢谢,但仍然收到“属性 contentInsetStartWithNavigation 没有资源标识符”错误。
      • @CHarris 你可以分享你的 gradle 依赖问题吗,因为相同的代码对我来说工作正常
      • 那里,在问题末尾添加了 gradle 依赖项。
      • tx 让它工作了,但 contentInsetEnd 对我改变了多少没有影响,最终接受了 SamyBs 的答案,因为它有效并且很好的练习让我自己去做并了解发生了什么。
      猜你喜欢
      • 1970-01-01
      • 2017-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多