繁体   English   中英

如何在Android中的ActionBar中删除左填充边距的额外空间?

[英]How to remove the left padding margin extra space in the ActionBar in Android?

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

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

Android:从操作栏的自定义布局中删除左边距

Android Lollipop、AppCompat ActionBar 自定义视图不占用整个屏幕宽度

Android API 21 工具栏填充

我的活动/操作栏左侧的填充/边距 - 不知道它来自哪里,无法删除它

如何删除默认布局的填充/边距

如何在 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,它几乎是空的,只是主要活动类的虚拟布局,自定义操作栏视图位于另一个布局文件中。

<?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();
            }
        });
    }
}

可以在此处找到存在此问题的示例应用程序

你应该使用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);

尚未尝试使用工具栏setContentInsetsAbsolute函数,因为它仅适用于API 21或更高版本。 我的应用程序的最低目标是API 15。

用户Nilesh Senta在这方面的解决方案( 额外保证金问题 )是更新风格,它对我有用! 以下是我的代码

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM