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

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

将这些

行添加到工具栏 xml 中

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

将样式设置为工具栏:

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

把它放在工具栏 xml 中

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

你可以试试这个:

在创建中:

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"

这将为您创建后退按钮。

最新更新