【问题标题】:LinearLayout with dividers on pre Honeycomb预蜂窝上带有分隔线的 LinearLayout
【发布时间】:2012-08-22 03:04:48
【问题描述】:

从 API 级别 11 开始,setDividerDrawable()setShowDividers() 被引入 LinearLayout,使线性布局能够显示子元素之间的分隔线。我真的很想使用这个功能,但我也是针对 Honeycomb 之前的设备(API 级别

解决此问题的一种方法是扩展 LinearLayout 并手动添加分隔线。这是一个原型:

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class DividerLinearLayout extends LinearLayout
{
    public DividerLinearLayout(Context context)
    {
        super(context);
    }

    public DividerLinearLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public DividerLinearLayout(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    public void addView(View child)
    {
        if(super.getChildCount() > 0)
        {
            super.addView(LayoutInflater.from(getContext()).inflate(R.layout.divider, null));
        }
        super.addView(child);
    }
}

但是,使用这样的实现会改变任何客户端迭代子节点的行为。有些视图是客户自己插入的,有些是DividerLinearLayout 插入的。如果用户在指定索引处插入视图,也会出现问题。可以实现索引的转换,但是如果做错了,这可能会导致严重的错误。另外,我认为需要覆盖更多的方法。

有没有更好的方法来解决这个问题?是否有人已经开发了一个可免费使用的DividerLinearLayout 等价物?它似乎不存在于 Android 的兼容性库中。

【问题讨论】:

    标签: android android-layout android-linearlayout android-support-library android-compatibility


    【解决方案1】:

    如果我没记错的话,ActionBarSherlock 库已经实现了这一点,以提供向后兼容的 ActionBar 选项卡。您可能想先包含该库,然后再试一试。

    这是特定类 (com.actionbarsherlock.internal.widget.IcsLinearLayout) 的 the code

    【讨论】:

    • 我已经在使用 ActionBarSherlock,所以IcsLinearLayout 是一个简单的替代品。它就像一个魅力 - 感谢您指点它。
    • 在xml中使用时似乎有错误。另外,您提供的链接也失效了。
    【解决方案2】:

    IcsLinearLayout 是内部的,由于 ActionBarSherlock 将不再更新,建议使用 Google 的一种,称为“LinearLayoutICS”。

    阅读here了解如何使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 2014-11-01
      • 1970-01-01
      • 2023-03-10
      相关资源
      最近更新 更多