2016-09-28 77 views
1

我的活动如何删除操作栏中徽标左侧的填充?

<?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: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> 

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

我Mainfest文件

<activity 
    android:theme="@style/AppTheme.NoActionBar" 
    android:windowSoftInputMode="adjustResize|stateAlwaysHidden"> 
    <intent-filter> 

     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 

我尝试了很多事情,但似乎没有任何事情,为什么我上传这个问题这就是工作。我删除了填充,甚至尝试了contentInsets,但没有任何工作。

I want to remove that yellow space

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_navigate); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     toolbar.setLogo(R.drawable.ic_account_circle_black); 

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.addDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
} 
} 

drawer_layout

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 
    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:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_navigation_to_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_navigation_to_main" 
     app:menu="@menu/activity_navigation_to_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 
+0

查看我的回答 –

+0

查看我的更新回答 –

+0

非常感谢你的先生。 – Ashish

回答

1

您可以使用内容插入功能。 左侧插图由工具栏的contentInsetStart引起,默认情况下为16dp。

试试这个

<android.support.v7.widget.Toolbar 
    xmlns:app="schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/primaryColor" 
    app:contentInsetLeft="0dp" 
    app:contentInsetStart="0dp" 
    app:contentInsetRight="0dp" 
    app:contentInsetEnd="0dp" /> 

而在你的根添加此。

xmlns:app="schemas.android.com/apk/res-auto"; 

在你的Android代码

Toolbar.setContentInsetStartWithNavigation(0); 

尝试这种在最坏的情况下尝试。 在您的活动onCreate中添加以下代码。

ImageView view = (ImageView)findViewById(android.R.id.home); 
view.setPadding(10, 0, 0, 0); 

让我知道它是否有效。

+0

不工作,它应该是xmlns:app =“http://schemas.android.com/apk/res-auto”;这已经在那里了。 – Ashish

+0

@Ashish你能分享你的活动代码吗? –

+0

我编辑了我的问题,请看看它@ragu – Ashish

0

默认情况下左边插入的是16dp,但是您正在添加图标,这就是为什么左侧插入现在为72dp。您可以通过将以下代码添加到Toolbar进行更改。

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

OR

您还可以设置以编程方式如下填充

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
int padding = 50; 
toolbar.setPadding(padding, toolbar.getPaddingTop(), padding, toolbar.getPaddingBottom()); 

一个更重要的注意事项:

您可以添加不IconTitle两次。如在文档 中提到的,您一次可以添加一件事Either (Icon) or (Title)

+0

我显示的截图工作正常,当我试图在同一时间添加两个。我已经尝试删除填充和设置contentInsetLeft,并开始不帮助我, – Ashish