4

バックグラウンド

Androidの古いバージョンでも、linearLayoutの仕切り機能を使用しようとしています。

このため、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>

仕切り.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 コードから 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);
      }
    }

質問

横向きでも機能させるにはどうすればよいですか?

4

2 に答える 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>
于 2013-12-27T17:33:03.233 に答える
0

これを簡単に行うことができます(Android API 10電話(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>

これは回避策ですが、機能します。

于 2013-12-29T02:48:49.430 に答える