在 android 中减少工具栏导航图标的左边距

reduce left margin of navigation icon of toolbar in android

因为我是 android 的新手,我在工具栏中设置了后退按钮,我想从左侧减少它的 padding/margin 我该怎么做? 提前致谢。

这是我的设计代码

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

这是我的屏幕(后退按钮有左边距)

将其放在工具栏中 xml

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

你可以试试这个:

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

在清单中添加这个 activity 你想要后退按钮在:

android:parentActivityName=".homeActivity"

这将为您创建后退按钮。

将样式设置为工具栏:

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

然后在 activity 的 Create 方法中执行此操作,

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

将这些行添加到您的工具栏中 xml

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