2016-06-23 25 views
5

Với Android mới 24, tôi phát hiện ra rằng biểu tượng và tiêu đề trên thanh ứng dụng có đệm rộng hơn và tôi không thể tìm thấy bất kỳ cách nào Giải quyết điều này.Padding/Space Application bar giữa Icon và Title (Android 24)

Ví dụ:

Additional space between icon and title

MainActivity.java:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setTitle("Testing"); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
    } 

} 

chính Hoạt động Layout (.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="house.appbartesting.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

    <include layout="@layout/content_main" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@android:drawable/ic_dialog_email" /> 

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

.....

Trả lời

17

Bạn có thể thêm thuộc tính này vào thanh công cụ để tránh vùng đệm này.

app:contentInsetStartWithNavigation="0dp" 
+0

Đó xảy ra với tôi khi tôi nâng cấp phiên bản SDK của tôi 23-24 adn này cố định nó. Cảm ơn bạn. –

2
Use below code to remove the extra spacing generated between back arrow and title 
for Android 24 (buildToolsVersion "24"/targetSdkVersion 24) 

Do not remove following lines - 

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

Code - 

<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/red" 
    android:minHeight="?attr/actionBarSize" 
    app:contentInsetLeft="0dp" 
    app:contentInsetStart="0dp" 
    app:contentInsetStartWithNavigation="0dp" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

</android.support.v7.widget.Toolbar>