【问题标题】:Android ignores layout_weight parameter from styles.xmlAndroid 忽略 style.xml 中的 layout_weight 参数
【发布时间】:2014-06-14 07:36:03
【问题描述】:

看起来 android 忽略了 ImageButton 的布局参数。当我的布局 xml 使用样式定义 ImageButtons 时,会考虑 layout_weight。但是,当我动态添加按钮时,权重会被忽略,我必须手动定义 LayoutParameters。

Styles.xml 条目:

<style name="TabbedPanelToolbarControl" parent="android:style/Widget.ImageButton">
    <item name="android:background">@null</item>
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">1.0</item>
    <item name="android:paddingTop">5dp</item>
    <item name="android:paddingBottom">5dp</item>
</style>

布局xml:

...
<LinearLayout
    android:id="@+id/toolbar"
    android:orientation="vertical" >
    <!-- image buttons go here -->
</LinearLayout>
...

代码:

View view = inflater.inflate(R.layout.test, container, false);
LinearLayout mToolbar = (LinearLayout)view.findViewById(R.id.toolbar);

ImageButton ib = new ImageButton(this.getActivity(), null, R.style.TabbedPanelToolbarControl);

ib.setImageDrawable(icon);
ib.setContentDescription(title);

// ignored layout_weight defined in TabbedPanelToolbarControl style
mToolbar.addView(ib);
// need to re-specify layout
mToolbar.addView(ib, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));

有人知道在动态添加按钮时强制从styles.xml 中读取layout_weight 的方法吗?

【问题讨论】:

    标签: android android-styles android-imagebutton android-layout-weight


    【解决方案1】:

    在构造函数中读取布局参数不起作用。原因很合乎逻辑——当你创建ImageButton

    new ImageButton(this.getActivity(), null, R.style.TabbedPanelToolbarControl);
    

    此构造函数将读取样式属性。但是,由于它不在LinearLayout(或任何其他父级)内它不知道它必须实例化哪个LayoutParams。由于通用ViewGroup.LayoutParams 没有weight 属性,因此将被忽略。

    当通过LayoutInflater 充气时,情况完全不同。 LayoutInflater类中的相关代码是这样的:

    final View view = createViewFromTag(parent, name, attrs);
    final ViewGroup viewGroup = (ViewGroup) parent;
    final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
    

    由于viewGroup 变量是完全创建的LinearLayout,它具有覆盖generateLayoutParams() 以读取weightgravity

    【讨论】:

      【解决方案2】:

      您可以从样式中获取布局参数,然后使用这些参数添加ImageButton

          ...
          TypedArray a = obtainStyledAttributes(R.style.TabbedPanelToolbarControl, 
                  new int[] {android.R.attr.layout_width,
                  android.R.attr.layout_height,
                  android.R.attr.layout_weight});
      
      
          int width = a.getInt(0, LayoutParams.WRAP_CONTENT);
          int height = a.getInt(1, LayoutParams.WRAP_CONTENT);
          float weight = a.getFloat(2, 1);
      
          mToolbar.addView(ib, new LayoutParams(width, height, weight));
      

      【讨论】:

      • 很好,它奏效了。不考虑重量仍然很奇怪。它可能与 ViewGroup 没有 weight 参数并且只有 LinearLayout 有它有关。
      • @olegr 我有类似的问题,样式中没有考虑 paddingBottom。我正在加载样式。你知道为什么吗?
      • @Ardoramor 尝试使用ib.setLayoutParams(new LayoutParams(width, height, weight)); mToolbar.addView(ib); 它有一点不同的实现。我不确定,但似乎应该可以。
      • @Lena Bru 奇怪的是它没有被考虑。无需额外努力即可工作。尝试用您的代码示例提出问题。
      • @olegr 我确实问过这里的问题是链接stackoverflow.com/q/24182411/2136812
      猜你喜欢
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      相关资源
      最近更新 更多