Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 在水平LinearLayout上使用分隔符用于预蜂窝版本大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
背景

我正在尝试使用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://scheR_784_11845@as.android.com/apk/res/android"
    xmlns:tools="http://scheR_784_11845@as.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://scheR_784_11845@as.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://scheR_784_11845@as.android.com/apk/res/android"
xmlns:tools="http://scheR_784_11845@as.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上使用分隔符用于预蜂窝版本全部内容,希望文章能够帮你解决android – 在水平LinearLayout上使用分隔符用于预蜂窝版本所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。