【问题标题】:How to Remove left space on custom tool bar in android如何在android中删除自定义工具栏上的剩余空间
【发布时间】:2015-12-22 03:25:54
【问题描述】:

我在我的 android 应用程序中创建了自定义工具栏。在我的应用程序中,当我看到布局显示正确但运行应用程序时从左侧显示更多边距。我在我的 Activity 中扩展 AppCompatActivity。 我已将以下代码用于自定义工具栏和 XML

代码

ActionBar actionBar = getSupportActionBar(); // 将自定义视图添加到操作栏

    actionBar.setCustomView(R.layout.custom_actionbar);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/nav_headerbg" >

   <ImageButton
        android:id="@+id/btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/backbtn"
        android:contentDescription="@string/clear" />
    <ImageButton
        android:id="@+id/btnHome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/btnBack"
        android:background="@drawable/ico_nav"
        android:contentDescription="@string/clear" />

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Setting"
        android:textColor="#fff"
        android:textSize="20sp" />

    <ImageButton
        android:id="@+id/btnDeviceList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="1dp"
        android:padding="5dp"
        android:background="@drawable/headerdevicelist"
        android:contentDescription="@string/clear" />

</RelativeLayout>

请查找附件。我想删除标记为红色的剩余空间。如何修复它并建议我。

【问题讨论】:

  • 这是为“setHomeButtonEnabled”保留的空间吗?你试过禁用它吗?

标签: android appcompatactivity


【解决方案1】:

在您的应用中添加自定义操作栏样式:

<style name="MyActionBar" parent="Widget.AppCompat.ActionBar">
  <item name="contentInsetStart">0dp</item>
  <item name="contentInsetEnd">0dp</item>
</style>

并将其设置在您的应用程序主题中:

<style (your application theme)>
  <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-20
    相关资源
    最近更新 更多