2016-04-21 76 views
2

因为我是新来的android我在我的工具栏中设置了后退按钮,我想从左边减少它的填充/边距我怎么做? 感谢您的支持。减少工具栏中的左侧导航图标的边距android

这是我设计的代码

<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"> 
<TextView 
android:id="@+id/tripidtoolbar" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="left" 
android:text="name" 
android:textColor="#ffffff" 
android:textSize="18sp" 
android:textStyle="bold" 
/> 
<TextView 
android:id="@+id/dayNotoolbar" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="right" 
android:textColor="#ffffff" 
android:layout_marginRight="10dp" 
android:text="day" 
android:textAllCaps="true" 
android:textSize="18sp" 
android:textStyle="bold" 
/> 
</android.support.v7.widget.Toolbar> 

这是怎么了手动设置后退按钮

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_vouchers_details); 
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
toolbar.setNavigationIcon(R.drawable.arrowbackwhite); 
toolbar.setNavigationOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
onBackPressed(); 
} 
}); 
setSupportActionBar(toolbar); 

这是我屏幕(后退按钮就吃留有余量)

+1

可能[Android的副本:从操作栏的自定义布局中删除左边距](http:// st ackoverflow.com/questions/27354812/android-remove-left-margin-from-actionbars-custom-layout) –

+0

@EvgeniyMishustin:我试过这个问题,但仍然没有解决我的问题.. –

回答

0

把它放在工具栏中xml

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

..感谢回复....它看起来在Xml Editor中有效。但是当我在真实设备上运行应用程序时,它没有任何改变。它看起来是这样..你知道它为什么happans? –

-3

你可以试试这个:

中的onCreate

actionBar = getActionBar(); 
actionBar.setHomeButtonEnabled(true); 

然后补充一点:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case android.R.id.home: 
      onBackPressed(); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
    } 
} 

在你想在后退按钮清单活动补充一点:

android:parentActivityName=".homeActivity" 

这将为您创建后退按钮。

+1

我已经创建了后退按钮我的魔杖,以减少默认按钮左侧的空间。 –

0

设置样式为工具栏:

<!-- Tool Bar--> 
    <style name="ToolbarStyle" parent="@style/Widget.AppCompat.Toolbar"> 
     <item name="contentInsetStart">0dp</item> 
     <item name="android:paddingLeft">0dp</item> 
    </style> 

并在创建活动的方法,这样做,

getSupportActionBar().setDisplayShowTitleEnabled(false); 
getSupportActionBar().setDisplayHomeAsUpEnabled(false); 
4

添加这些行到您的工具栏的XML

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