【问题标题】:How to add dividers to LinearLayoutICS?如何将分隔线添加到 LinearLayoutICS?
【发布时间】:2014-03-21 00:25:39
【问题描述】:

背景

我正在尝试将我的 alternative "App Manager" appActionBarSherlock library 切换到 Google 创建的 support library,因为它得到了更多更新(ActionBarSherlock 不再被开发,链接 here),我认为它应该涵盖很多功能。

问题

一切都很顺利(或者看起来如此),除了我用来显示分隔线的 ActionBarSherlock 上名为 ICSLinearLayout 的类,现在称为 LinearLayoutICS。

它只是不显示分隔符:

注意:在你问“为什么不直接使用 GridView?”之前,here's 原因,还有this,以防我想添加标题。

代码

代码与我用于 ActionBarSherlock 的代码大致相同:

rowLayout=new LinearLayoutICS(_context,null);
rowLayout.setMeasureWithLargestChildEnabled(true);
rowLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
rowLayout.setDividerDrawable(_context.getResources().getDrawable(R.drawable.list_divider_holo_dark));
rowLayout.setOrientation(LinearLayout.HORIZONTAL);
... // add views, layout params, etc...

问题

我如何使用这个类来支持在所有支持的操作系统版本的支持库上显示分隔符?

我写的代码有什么问题?

【问题讨论】:

    标签: android actionbarsherlock android-linearlayout android-support-library divider


    【解决方案1】:

    好的,setShowDividerssetDividerDrawable 似乎不能使用,因为 LinearLayoutICS 没有它们。

    不仅如此,Lint 并没有警告我它正在被使用。

    所以,我最终得到的是复制 LinearLayoutICS 代码(来自 here,希望它是最新版本)和一些原始的 LinearLayout 代码,以使某些东西可以工作。我希望它没有任何错误。

    开源万岁... :)

    遗憾的是,setMeasureWithLargestChildEnabled 不适用于旧 API,因此我认为 ActionBarSherlock 方式在您希望使用的情况下仍然更好。

    编辑:setMeasureWithLargestChildEnabled 方法在 ActionBarSherlock 上不起作用。

    这是代码,供希望使用的人使用。我希望下次图书馆更新时,我会记得再次检查这个问题。

    public class LinearLayoutICS extends LinearLayout
      {
      private Drawable mDivider;
      private int      mDividerWidth,mDividerHeight;
      private int      mShowDividers;
      private int      mDividerPadding;
    
      public LinearLayoutICS(final Context context,final AttributeSet attrs)
        {
        super(context,attrs);
        // the R is from "android.support.v7.appcompat.R" .
        final TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.LinearLayoutICS);
        mDivider=a.getDrawable(R.styleable.LinearLayoutICS_divider);
        if(mDivider!=null)
          {
          mDividerWidth=mDivider.getIntrinsicWidth();
          mDividerHeight=mDivider.getIntrinsicHeight();
          }
        else mDividerHeight=mDividerWidth=0;
        mShowDividers=a.getInt(R.styleable.LinearLayoutICS_showDividers,SHOW_DIVIDER_NONE);
        mDividerPadding=a.getDimensionPixelSize(R.styleable.LinearLayoutICS_dividerPadding,0);
        a.recycle();
        setWillNotDraw(mDivider==null);
        }
    
      @Override
      protected void onDraw(final Canvas canvas)
        {
        if(getOrientation()==VERTICAL)
          drawDividersVertical(canvas);
        else drawDividersHorizontal(canvas);
        }
    
      @Override
      protected void measureChildWithMargins(final View child,final int parentWidthMeasureSpec,final int widthUsed,final int parentHeightMeasureSpec,final int heightUsed)
        {
        if(mDivider!=null)
          {
          final int childIndex=indexOfChild(child);
          final int count=getChildCount();
          final LayoutParams params=(LayoutParams)child.getLayoutParams();
          // To display the dividers in-between the child views, we modify their margins
          // to create space.
          if(getOrientation()==VERTICAL)
            {
            if(hasDividerBeforeChildAt(childIndex))
              params.topMargin=mDividerHeight;
            else if(childIndex==count-1&&hasDividerBeforeChildAt(count))
              params.bottomMargin=mDividerHeight;
            }
          else if(hasDividerBeforeChildAt(childIndex))
            params.leftMargin=mDividerWidth;
          else if(childIndex==count-1&&hasDividerBeforeChildAt(count))
            params.rightMargin=mDividerWidth;
          }
        super.measureChildWithMargins(child,parentWidthMeasureSpec,widthUsed,parentHeightMeasureSpec,heightUsed);
        }
    
      void drawDividersVertical(final Canvas canvas)
        {
        final int count=getChildCount();
        for(int i=0;i<count;i++)
          {
          final View child=getChildAt(i);
          if(child!=null&&child.getVisibility()!=GONE&&hasDividerBeforeChildAt(i))
            {
            final LayoutParams lp=(LayoutParams)child.getLayoutParams();
            drawHorizontalDivider(canvas,child.getTop()-lp.topMargin);
            }
          }
        if(hasDividerBeforeChildAt(count))
          {
          final View child=getChildAt(count-1);
          int bottom=0;
          if(child==null)
            bottom=getHeight()-getPaddingBottom()-mDividerHeight;
          else bottom=child.getBottom();
          drawHorizontalDivider(canvas,bottom);
          }
        }
    
      void drawDividersHorizontal(final Canvas canvas)
        {
        final int count=getChildCount();
        for(int i=0;i<count;i++)
          {
          final View child=getChildAt(i);
          if(child!=null&&child.getVisibility()!=GONE&&hasDividerBeforeChildAt(i))
            {
            final LayoutParams lp=(LayoutParams)child.getLayoutParams();
            drawVerticalDivider(canvas,child.getLeft()-lp.leftMargin);
            }
          }
        if(hasDividerBeforeChildAt(count))
          {
          final View child=getChildAt(count-1);
          int right=0;
          if(child==null)
            right=getWidth()-getPaddingRight()-mDividerWidth;
          else right=child.getRight();
          drawVerticalDivider(canvas,right);
          }
        }
    
      void drawHorizontalDivider(final Canvas canvas,final int top)
        {
        mDivider.setBounds(getPaddingLeft()+mDividerPadding,top,getWidth()-getPaddingRight()-mDividerPadding,top+mDividerHeight);
        mDivider.draw(canvas);
        }
    
      void drawVerticalDivider(final Canvas canvas,final int left)
        {
        mDivider.setBounds(left,getPaddingTop()+mDividerPadding,left+mDividerWidth,getHeight()-getPaddingBottom()-mDividerPadding);
        mDivider.draw(canvas);
        }
    
      /**
       * Determines where to position dividers between children.
       *
       * @param childIndex Index of child to check for preceding divider
       * @return true if there should be a divider before the child at childIndex
       * @hide Pending API consideration. Currently only used internally by the system.
       */
      protected boolean hasDividerBeforeChildAt(final int childIndex)
        {
        if(childIndex==0)
          return (mShowDividers&SHOW_DIVIDER_BEGINNING)!=0;
        else if(childIndex==getChildCount())
          return (mShowDividers&SHOW_DIVIDER_END)!=0;
        else if((mShowDividers&SHOW_DIVIDER_MIDDLE)!=0)
          {
          boolean hasVisibleViewBefore=false;
          for(int i=childIndex-1;i>=0;i--)
            if(getChildAt(i).getVisibility()!=GONE)
              {
              hasVisibleViewBefore=true;
              break;
              }
          return hasVisibleViewBefore;
          }
        return false;
        }
    
      @Override
      public int getDividerPadding()
        {
        return mDividerPadding;
        }
    
      @Override
      public void setDividerPadding(final int dividerPadding)
        {
        mDividerPadding=dividerPadding;
        }
    
      @Override
      public void setShowDividers(final int showDividers)
        {
        if(mShowDividers!=showDividers)
          requestLayout();
        mShowDividers=showDividers;
        }
    
      @Override
      public void setDividerDrawable(final Drawable divider)
        {
        if(divider==mDivider)
          return;
        mDivider=divider;
        if(divider!=null)
          {
          mDividerWidth=divider.getIntrinsicWidth();
          mDividerHeight=divider.getIntrinsicHeight();
          }
        else
          {
          mDividerWidth=0;
          mDividerHeight=0;
          }
        setWillNotDraw(divider==null);
        requestLayout();
        }
    
      @Override
      public Drawable getDividerDrawable()
        {
        return mDivider;
        }
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-16
      • 2013-08-20
      • 1970-01-01
      • 2019-01-27
      • 2017-04-04
      • 2017-02-01
      • 2015-09-23
      • 1970-01-01
      相关资源
      最近更新 更多