【发布时间】: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官方代码中复制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);
}
}
问题
如何让它也适用于水平方向?
【问题讨论】:
标签: android actionbarsherlock android-linearlayout android-3.0-honeycomb divider