背景
我正在尝试使用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);
}
}
问题
如何使它在水平方向上也能正常工作?