简体   繁体   English

使用可影响布局的图层列表可设置标签的背景

[英]Setting a tab's background with a layer-list drawable affecting layout

I'm trying to set a custom background for the tabs in my TabWidget. 我正在尝试为TabWidget中的标签设置自定义背景。 They're pretty much all just a bunch of rectangle shape gradients, but I found out that by setting the background drawables, it removes the default divider between each tab. 它们几乎都是一堆矩形形状的渐变,但是我发现通过设置背景可绘制对象,它可以删除每个选项卡之间的默认分隔线。 To remedy this, I used a layer-list drawable to layer a divider on top of the tabs. 为了解决这个问题,我使用了一个可绘制的图层列表,在选项卡的顶部分层了一个分隔线。 However, now, it's affecting the layout of the tab itself, causing the image and text label to move apart. 但是,现在,它影响了选项卡本身的布局,导致图像和文本标签分开。

Intended result 预期结果
What I get by using a layer-list for the background 我通过使用图层列表获得的背景

I'm just using this to setup the backgrounds: 我只是用它来设置背景:

TabWidget tw = mTabHost.getTabWidget(); 
for (int i = 0; i < tw.getChildCount(); i++) { 
    View v = tw.getChildAt(i); 
    v.setBackgroundDrawable(getResources().getDrawable (R.drawable.tab_background)); 
}

Any clues as to why this is happenings, or suggestions on what else I can do instead? 关于发生这种情况的任何线索,或者关于我还能做些什么的建议?

It seems that using the layer-list as a background reset the all of the paddings within the view to 0. A bit non-sensical, considering a background drawable conceptually shouldn't affect any of this stuff, but whatever, I suppose. 似乎使用layer-list作为背景会将视图中的所有填充都重置为0。考虑到背景可绘制的概念,这有点荒谬,但我想应该不会影响任何这些东西。 Saving the previous paddings and setting them back to what they were before fixes the problem. 保存以前的填充并将其设置回原来的位置即可解决此问题。

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

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