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

我正在使用自定义的操作栏视图,正如您在下面的屏幕截图中所看到的,操作栏中有一个空白的空白区域。 我想删除它。

在这里输入图像说明

我做了什么:

RES /值-V11 / styles.xml

 @style/ActionBarStyle @style/ActionBarStyle  

RES /值/ my_custom_actionbar.xml

   60dp   

表现

     

主要活动

 public void onCreate(Bundle bundle) { super.onCreate(bundle); ActionBar actionbar = getSupportActionBar(); actionbar.setDefaultDisplayHomeAsUpEnabled(false); actionbar.setDisplayHomeAsUpEnabled(false); actionbar.setDisplayShowCustomEnabled(true); actionbar.setDisplayShowHomeEnabled(false); actionbar.setDisplayShowTitleEnabled(false); actionbar.setDisplayUseLogoEnabled(false); actionbar.setHomeButtonEnabled(false); // Add the custom layout View view = LayoutInflater.from(this).inflate(R.layout.actionbar, null, false); actionbar.setCustomView(view); } 

我发现最近的一篇文章,指出最新版本存在一个问题。 我也更新了ADT和SDK到Android 5。

Android ActionBar的自定义视图不填充父项

我不知道该怎么办。

编辑(部分解决方案):

不适用于Android <= API 10。

Android棒棒糖,AppCompat的ActionBar自定义视图不占用整个屏幕宽度

我改变了什么:

使用最新的sdk版本:

  

添加一个toolbarStyle

  @style/ActionBarStyle @style/ActionBarStyle @style/ToolbarStyle @style/ToolbarStyle   0dp 0dp  

如果您要通过XML添加Toolbar ,则只需添加XML属性即可删除内容插页。

  

尝试这个:

  ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowCustomEnabled(true); actionBar.setDisplayShowTitleEnabled(false); View customView = getLayoutInflater().inflate(R.layout.main_action_bar, null); actionBar.setCustomView(customView); Toolbar parent =(Toolbar) customView.getParent(); parent.setPadding(0,0,0,0);//for tab otherwise give space in tab parent.setContentInsetsAbsolute(0,0); 

我在我的项目中使用了这个代码,祝你好运。

我发现了另一个解决方案(参考appcompat-v7),它改变了toolbarStyle,代码如下:

 @style/Widget.Toolbar  

左边的插图是由Toolbar的contentInsetStart引起的,默认是16dp。

将其更改为与角线对齐。

更新支持库v24.0.0:

为了匹配材质设计规范,还有一个额外的属性contentInsetStartWithNavigation ,默认情况下是16dp。 如果您还有导航图标,请更改此项。

事实certificate,这是设计库版本24中引入的新材料设计规范的一部分。

https://material.google.com/patterns/navigation.html

但是,可以通过将以下属性添加到工具栏小部件来删除多余的空间。

 app:contentInsetStartWithNavigation="0dp" 

之前: 在这里输入图像说明

之后: 在这里输入图像说明

  

这应该是够好的。

只需修改你的styles.xml

   

而不是在布局中添加工具栏,您可以设置您的自定义视图,如下所示。

 Toolbar parent = (Toolbar) customView.getParent(); parent.setContentInsetsAbsolute(0,0); 

使用AppCompatAcitivty你可以使用

 Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar); View logo = getLayoutInflater().inflate(R.layout.custom_toolbar, null); mToolbar.addView(logo, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); mToolbar.setContentInsetsAbsolute(0,0); 

只添加app:contentInsetStart="0dp"到工具栏删除左侧的空间。

所以你的工具栏定义看起来像这样

   

它看起来像这样

在这里输入图像说明

您需要在工具栏中添加这行app2:contentInsetStart =“0dp”

  

正确和最简单的答案是在另一篇文章,这是:

Android棒棒糖,AppCompat的ActionBar自定义视图不占用整个屏幕宽度

它被标记为正确的,并且在我尝试之后,我可以确认它是有效的。

像这样创建工具栏:

  

请按照此链接更多 – Android的提示

只添加android:padding="0dp"为我工作

  
 ActionBar ab = getSupportActionBar(); ab.setDisplayShowHomeEnabled(true); ab.setDisplayShowCustomEnabled(true); setDisplayShowTitleEnabled(true); View customView = getLayoutInflater().inflate(R.layout.activity_main,null); //here activity_main.xml is the GUI design file. ab.setCustomView(customView); Toolbar parent =(Toolbar) customView.getParent(); //use V7 Toolbar import parent.setContentInsetsAbsolute(0, 0); setPadding(5,0,0,0); ab.setIcon(R.mipmap.ic_launcher); 

非常简单的方法可以删除工具栏左边的填充,请使用这种方式

  

它看起来很酷,看图像

在这里输入图像说明

您可以在xml文件中使用工具栏视图组内的相对布局,并根据您的用例调整小部件的位置。无需创建自定义布局并将其膨胀并附加到工具栏。 一旦在你的java代码中完成,使用setContentInsetsAbsolute(0,0)与你的工具栏对象,然后将其设置为布局中的支持操作栏。

将后台项目添加到应用程序操作栏的样式中以与自定义操作栏的背景色一致将会更好:

 @color/actionbar_bgcolor 

在Android 6.0之后,操作栏甚至有一个空白右边的空间,不能设置。 在这样的活动中添加一个右边距调整:( 视图是自定义的操作栏或其中的一个右键)

 int rightMargin = Build.VERSION.SDK_INT>=Build.VERSION_CODES.M ? 0 : 8; // may the same with actionbar leftMargin in px ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) view.getLayoutParams(); p.setMargins(p.leftMargin, p.topMargin, rightMargin, p.bottomMargin); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){ p.setMarginEnd(rightMargin); } view.setLayoutParams(p); 

Android 3.0+应用程序仅支持actionbar machenism。 如果我们使用工具栏(支持lib v7),我们应该在每个活动的每个xml中进行布局,并注意在Android 5.0或更高版本的设备中与系统状态栏重叠的问题。

科特林

  supportActionBar?.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM; supportActionBar?.setCustomView(R.layout.actionbar); val parent = supportActionBar?.customView?.parent as Toolbar parent?.setPadding(0, 0, 0, 0)//for tab otherwise give space in tab parent?.setContentInsetsAbsolute(0, 0) 

我在任何地方都找不到解决问题的办法(第一张图片),但最终在经过几个小时的挖掘后,最终得到了一个最简单的解决方案。 请注意,我尝试了很多xml属性,如app:setInsetLeft="0dp"等等,但是在这种情况下没有一个帮助。

图片1 在这里输入图像说明

下面的代码解决了这个问题,如图2所示

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); //NOTE THAT: THE PART SOLVED THE PROBLEM. android.support.design.widget.AppBarLayout abl = (AppBarLayout) findViewById(R.id.app_bar_main_app_bar_layout); abl.setPadding(0,0,0,0); } 

图片2

在这里输入图像说明

你的应用程序是否支持android 5? 如果不支持,则可以降级支持actionbar v7的版本。 这看起来像:

编译“com.android.support:appcompat-v7:19.0.+”(不要使用v7:21+)

也使目标版本的应用程序少于21。

targetSdkVersion 14

如果你的应用程序支持android 5 – >你将需要自定义的工具栏。 (因为appcombat-v7:21有一些变化)。