【问题标题】:reduce left margin of navigation icon of toolbar in android减少android中工具栏导航图标的左边距
【发布时间】:2016-04-21 10:43:50
【问题描述】:

由于我是 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);

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

【问题讨论】:

标签: android navigation icons toolbar back


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    将样式设置为工具栏:

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

    【讨论】:

    • 要让它真正起作用,你还必须在你的主题中设置`@style/ToolbarStyle`
    【解决方案3】:

    把这个放在工具栏xml里

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

    【讨论】:

    • ..感谢您的回复....它看起来在 Xml 编辑器中工作。但是当我在真实设备中运行应用程序时,它没有任何变化。它看起来是这样的……你知道为什么会这样吗??
    【解决方案4】:

    你可以试试这个:

    在 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"
    

    这将为您创建返回按钮。

    【讨论】:

    • 我已经创建了后退按钮,我想减少默认情况下按钮左侧的空间。
    猜你喜欢
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 2016-11-13
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    相关资源
    最近更新 更多