【问题标题】:Using dividers on horizontal LinearLayout for pre-honeycomb versions在水平 LinearLayout 上使用分隔线用于预蜂窝版本
【发布时间】:2013-06-13 07:34:41
【问题描述】:

背景

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

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

问题

当你使用垂直方向时它工作正常,但如果你使用水平方向,它不会在下一个案例中显示分隔符:

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

现在,我知道不应该使用内部视图,但这是线性布局的一个非常重要的功能,我找不到任何好的替代方案(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>

divider.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);
      }
    }

问题

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

【问题讨论】:

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


    【解决方案1】:

    试试这个示例

    <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>
    

    【讨论】:

    • 我不明白你的解决方案。
    • 1.我尝试了我的代码,但没有成功。我的意思是修复 ABS 库的 IcsLinearLayout。并不意味着你会使用我的代码(它有错误) 2. 它已经应该根据分隔线的大小自动为孩子设置边距。
    【解决方案2】:

    您可以简单地执行此操作(在 Android api 10 手机 (2.3.3 - 2.3.6) 上测试):

    <LinearLayout 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:orientation="horizontal"
    tools:context=".MainActivity" >
    
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FFff0000" />
    
    <!-- First divider -->
    <View
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:background="@android:color/black" />
    
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FFffff00" />
    
    <!-- Second divider -->
    <View
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:background="@android:color/black" />
    
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FFff00ff" />
    
    </LinearLayout>
    

    这是一种解决方法,但它有效!

    【讨论】:

    • 当然可以,但这不是我要问的。我想利用与从 ICS 到所有 Android 版本的 LinearLayout 相同的功能...... :(
    猜你喜欢
    • 2012-08-22
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多