Using LinearLayout Horizontal Separators for Pre-Cellular Versions

Background

I am trying to use the divers function for linearLayout, even on older versions of android.

for this, I found out that actionBarSherlock has a class called " com.actionbarsherlock.internal.widget.IcsLinearLayout ".

Problem

it works fine when you use the vertical orientation, but if you use the horizontal orientation, in the following case, the delimiters are not displayed:

when Android is included in API 17 and above and the device uses an RTL language (for example, Hebrew) and you set android: supportsRtl = "true". this causes some delimiters to show (and some not), as well as an empty delimiter (like a field) to the left.

Now, I know that internal views should not be used, but this is a very important function for linearLayouts, and I cannot find any good alternative ( HoloEverywhere is a very heavy library and not granular enough for that).

here is a usage example:

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> 

again, if it is in a vertical orientation (and you set the width and height of the children correctly), it will perfectly display the delimiters.

What i tried

I tried to force it to ignore new versions and apply them only to old ones (both checking versions and avoiding calling new API functions), but this did not help.

I also tried to copy the drawDividersHorizontal part from the official Android code, as such:

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

Question

How can I make it work for horizontal orientation?

+4
source share
2 answers

try this sample

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

You can simply do this (tested on Android api 10 phone (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> 

This is a workaround, but it works!

0
source

Source: https://habr.com/ru/post/1485946/


All Articles