android - 在蜂窝前版本的水平 LinearLayout 上使用分隔线

标签 android actionbarsherlock android-linearlayout android-3.0-honeycomb divider

背景

我正在尝试使用 linearLayout 的分隔线功能,即使是在旧版本的 android 上也是如此。

为此,我发现 actionBarSherlock有一个很好的类叫做“com.actionbarsherlock.internal.widget.IcsLinearLayout”。

问题

当你使用垂直方向时它工作正常,但如果你使用水平方向,它不会在下一种情况下显示分隔线:

当 Android 使用 API 17 及更高版本时,设备使用 RTL 语言(如希伯来语),并且您已设置 android:supportsRtl="true"。这会导致一些分隔线显示(有些不显示),以及左侧的空分隔线(如边距)。

现在,我知道不应该使用内部 View ,但这是 linearLayouts 的一个非常重要的特性,我找不到任何好的替代品(HoloEverywhere 是一个非常沉重的库,而且不是粒度的足以用于此)。

这是一个用法示例:

activity_main.xml

<com.example.test.IcsLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@drawable/divider"
    android:measureWithLargestChild="true"
    android:orientation="horizontal"
    android:showDividers="middle"
    tools:context=".MainActivity" >

    <View
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FFff0000" />

    <View
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FFffff00" />

    <View
        android:layout_width="0px"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FFff00ff" />

</com.example.test.IcsLinearLayout>

分隔符.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <size
        android:height="1dp"
        android:width="1dp" />

    <solid android:color="#FF000000" />

</shape>

同样,如果它处于垂直方向(并且您正确设置了子项的宽度和高度),它将很好地显示分隔线。

我尝试过的

我试图让它忽略新版本,只适用于旧版本(通过检查版本和避免调用新 API 的函数)但它没有帮助。

我也试过从Android官方代码中复制drawDividersHorizo​​ntal的部分,如下:

  void drawDividersHorizontal(final Canvas canvas)
    {
    final int count=getChildCount();
    boolean isLayoutRtl=false;
    if(VERSION.SDK_INT>=VERSION_CODES.JELLY_BEAN_MR1)
      isLayoutRtl=(getLayoutDirection()&View.LAYOUT_DIRECTION_RTL)!=0;
    for(int i=0;i<count;i++)
      {
      final View child=getChildAt(i);
      if(child!=null&&child.getVisibility()!=GONE)
        if(hasDividerBeforeChildAt(i))
          {
          final LayoutParams lp=(LayoutParams)child.getLayoutParams();
          final int position;
          if(isLayoutRtl)
            position=child.getRight()+lp.rightMargin;
          else position=child.getLeft()-lp.leftMargin-mDividerWidth;
          drawVerticalDivider(canvas,position);
          }
      }
    if(hasDividerBeforeChildAt(count))
      {
      final View child=getChildAt(count-1);
      int position;
      if(child==null)
        {
        if(isLayoutRtl)
          position=getPaddingLeft();
        else position=getWidth()-getPaddingRight()-mDividerWidth;
        }
      else
        {
        final LayoutParams lp=(LayoutParams)child.getLayoutParams();
        if(isLayoutRtl)
          position=child.getLeft()-lp.leftMargin-mDividerWidth;
        else position=child.getRight()+lp.rightMargin;
        }
      drawVerticalDivider(canvas,position);
      }
    }

问题

如何让它也适用于水平方向?

最佳答案

试试这个例子

<com.example.test.IcsLinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="@drawable/divider"
android:measureWithLargestChild="true"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".MainActivity" >

 <View
android:layout_width="2dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="3dip"
android:background="#FFff0000" />
<View
android:layout_width="2dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="3dip"
android:background="#FFff0000" />
<View
android:layout_width="2dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="3dip"
android:background="#FFff0000" />
</com.example.test.IcsLinearLayout>

关于android - 在蜂窝前版本的水平 LinearLayout 上使用分隔线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17081614/

相关文章:

xml - 布局 xml 崩溃应用程序中的 Android @+id

android - iOS:Instagram Api 抛出错误消息

Android ActionMode 动态替换 Action Items

android - 操作栏 Sherlock 中操作栏选项卡的偏移量不正确

android - 将复选框对齐到LinearLayout android中的右侧

java - 在 Android 应用程序中手动排列布局中的按钮

android - 如何在应用程序的 ImageView(主要 Activity )中从网络加载图像?

android - 共享 Action 提供者 :How to know which app is sharing the content?

Android线性布局问题

java - 迭代当前月份以获取所有事件