在早期版本的Android系统中,为了在水平LinearLayout中使用分割线,可以采用以下方法:

4

背景

我正在尝试使用linearLayout的分隔符功能,即使在较旧版本的Android上也能实现。

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

问题

当您使用垂直方向时,它可以正常工作,但是如果您使用水平方向,则不会显示下一个情况的分隔符:

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

现在,我知道不应该使用内部视图,但这对于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>

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代码中复制drawDividersHorizontal的部分,具体如下:

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

问题

如何使它在水平方向上也能正常工作?

2个回答

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>

我不理解你的解决方案。 - android developer
  1. 我尝试了我的代码,但它没有起作用。我是指修复ABS库的IcsLinearLayout,而不是使用我的代码(它有错误)。
  2. 它已经应该根据分隔符的大小自动为子元素设置边距。
- android developer

0
你可以简单地执行以下操作(在Android API10手机(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中LinearLayout使用的相同功能,在所有Android版本上实现... :( - android developer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接