【问题标题】:How to remove the left padding margin extra space in the ActionBar in Android?如何在Android中删除ActionBar中的左侧填充边距额外空间?
【发布时间】:2016-07-24 17:57:50
【问题描述】:

我一直在努力解决这个左边的填充/边距问题,只是不会消失,即将变得秃顶......对此的解决方案将不胜感激!我已经从 SO 尝试了这些答案,它确实移动了一些填充,但不是全部。基本上所有的答案都说将 contentInsetStart 和 contentInsetLeft 设置为 0dp。这确实删除了左侧的一些填充,但我仍然看到一个填充,它比我将 contentInsetStart 和 contentInsetLeft 设置为 0dp 之前要小。此解决方案适用于三星手机,但不适用于我的任何平板电脑经测试,如 Nexus 7、Nexus 9、Dell Venus 8 等。

我相信很多人都有这个问题,这些是我尝试过的答案,但仍然在上面显示的左侧留下了填充/边距。

Android: remove left margin from actionbar's custom layout

Android Lollipop, AppCompat ActionBar custom view doesn't take up whole screen width

Android API 21 Toolbar Padding

Padding/margin on the left side of my activity/actionbar - dont know where it comes from, cannot remove it

How to remove default layout's padding/margin

How to remove the margin between the app icon and the edge of the screen on the ActionBar?

风格

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="actionBarStyle">@style/AppThemeActionBar</item>
        <item name="logo">@android:color/transparent</item>
    </style>

    <style name="AppThemeActionBar" parent="@style/Widget.AppCompat.ActionBar">
        <item name="displayOptions">showCustom</item>
        <item name="background">@color/blue</item>
        <item name="actionBarSize">50dp</item>
        <item name="height">50dp</item>

        <item name="contentInsetLeft">0dp</item>
        <item name="contentInsetStart">0dp</item>
        <item name="contentInsetEnd">0dp</item>
        <item name="android:layout_marginLeft">0dp</item>
    </style>
</resources>

活动布局,activity_main.xml,它几乎是空的,只是 Main 活动类的一个虚拟布局,自定义操作栏视图在另一个布局文件中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

现在,这是自定义操作栏布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp" >

    <Button
        android:id="@+id/btn_home"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="#ea6464"
        android:text="Home"/>

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/btn_home"
        android:gravity="center"
        android:textColor="#fff"
        android:textStyle="bold"
        android:text="Hello"/>

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@android:drawable/ic_menu_edit"
        android:background="@null"/>
</RelativeLayout>

MainActivity.java 扩展自定义操作栏布局并设置它 mActionBar.setCustomView(mCustomView);

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar mActionBar = getSupportActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);

        LayoutInflater mInflater = LayoutInflater.from(this);
        View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);

        Button btnHome = (Button) findViewById(R.id.btn_home);

        btnHome.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "Home Button Clicked!", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

有这个问题的示例应用可以在here找到

【问题讨论】:

  • 我认为你应该使用工具栏而不是操作栏
  • 我用 Toolbar 试过了,它给出了同样的问题。
  • 所以如果工具栏或操作栏没有按照您的意愿行事,您可以创建一个类似工具栏的布局(可以使用RelativeLayout)并用作工具栏
  • 是的,这将是我最后的选择,但我想知道是否有使用操作栏或工具栏的解决方案,或者这是不可能的。
  • 我在 Android M 上遇到了同样的问题(仅在平板电脑上)。 @s-hunter 你找到解决办法了吗?

标签: android layout android-actionbar margin padding


【解决方案1】:

你应该使用ToolBar 而不是ActionBar 并调用setContentInsetsAbsolute 或者如果你真的想保留ActionBar,你应该这样做:

ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);

LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
Toolbar parent = (Toolbar) customView.getParent();
parent.setContentInsetsAbsolute(0,0);

【讨论】:

  • 我上面列出的答案确实有工具栏的解决方案,我已经尝试过了,但它仍然没有完全删除左边的填充。也有像你这样的答案,还是不行……
【解决方案2】:

没有尝试过 Toolbar setContentInsetsAbsolute 函数,因为它只适用于 API 21 或更高版本。我的应用的最低目标是 API 15。

用户 Nilesh Senta 的解决方案 (extra margin issue) 是更新样式,它对我有用!下面是我的代码

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionBarStyle">@style/Actionbar</item>
    <item name="android:titleTextStyle">@style/ActionbarTitle</item>
</style>

<style name="Actionbar" parent="Widget.AppCompat.ActionBar">
    <item name="android:titleTextStyle">@style/ActionbarTitle</item>
    <item name="contentInsetStart">0dp</item>
    <item name="contentInsetEnd">0dp</item>
</style>

java (onCreate)

ActionBar actionBar =  getSupportActionBar();

actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);

ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.MATCH_PARENT,
            ActionBar.LayoutParams.MATCH_PARENT
    );

View view = LayoutInflater.from(this).inflate(R.layout.actionbar_main, null);

actionBar.setCustomView(view, layoutParams); 

【讨论】:

    【解决方案3】:

    如果存在导航按钮,contentInsetsAbsolute 不是解决方案,或者至少是不够的。请尝试以下操作:

    val toolbar = customSearch.parent as Toolbar
    toolbar.setContentInsetStartWithNavigation(0)
    

    【讨论】: