2017-12-19 40 views
0

AndroidアプリでSwitchを回転しようとしています。私はandroid:rotationパラメータを認識していますが、これはアプリケーションの共通部分であるため、私はスイッチを拡張するカスタムビューを構築しています。デフォルトでは、ビューに回転を適用することは非回転のビューの元の寸法を維持するため、この実装では、新しい向きに合わせて、幅と高さのパラメータを切り替える必要があります。回転切り替えビューのクリッピング

public class VerticalSwitch extends Switch { 

// Init method called from all constructors 
    private void init(Context context, …) { 
     // Rotate the view 
     setRotation(switchOrientation.ordinal()*90); 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     int width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight(); 
     int height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom(); 

     int desiredWidth = height + getPaddingLeft() + getPaddingRight(); 
     int desiredHeight = width + getPaddingTop() + getPaddingBottom(); 

     //noinspection SuspiciousNameCombination 
     setMeasuredDimension(measureDimension(desiredWidth, widthMeasureSpec), 
       measureDimension(desiredHeight, heightMeasureSpec)); 
    } 

    private int measureDimension(int desiredSize, int measureSpec) { 
     int result; 
     int specMode = MeasureSpec.getMode(measureSpec); 
     int specSize = MeasureSpec.getSize(measureSpec); 

     if (specMode == MeasureSpec.EXACTLY) { 
      result = specSize; 
     } else { 
      result = desiredSize; 
      if (specMode == MeasureSpec.AT_MOST) { 
       result = Math.min(result, specSize); 
      } 
     } 

     if (result < desiredSize){ 
      Log.e(TAG, "The view is too small, the content might get cut"); 
     } 
     return result; 
    } 
} 

これは提案サイズを固定する方法を使用していますin this article by Lorenzo Quiroli。ここ

ない回転(図の境界がオンされている)を有する正常Switchビューの一連続い-90android:rotationパラメータと正常Switch、続いて、結果(第一のスイッチ)である。

A row of switches with view bounds shown. The first is vertical, but placed lower than the others with the bottom half cut off, the second is correctly vertical, and the remainder are normal horizontal switches.

ドローイングビューの境界から、回転を伴う通常のSwitchは、通常、水平スイッチの元の寸法を保持している境界の外にドロアブルが伸びるため、視覚的にクリップされることがわかります。ただし、カスタムVerticalSwitchは正しい高さを持っています(2番目のスイッチで完全なドロアブルが表示されます)。ただし、ドロアブルはビューの下半分にオフセットされています。水平な構成である。

デバッガでサイジングのパラメータを調べると、回転した新しい寸法が正しく適用されていることがわかりますが、クリッピングはまだ発生しています。オフセットとクリッピングの原因は何ですか?また、どのように修正できますか?垂直カスタムSwitchを作成する

答えて

1

の必要はありませんあなたはこの

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="50dp"> 

    <Switch 
     android:layout_width="wrap_content" 
     android:layout_height="60dp" 
     android:rotation="90" /> 

    <Switch 
     android:layout_width="wrap_content" 
     android:layout_height="60dp" 
     android:rotation="90" /> 


    <Switch 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Switch 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <Switch 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 


</LinearLayout> 

OUTPUT

enter image description hereを試してみてくださいあなたのSwitch にだけ静的な高さを与える必要が垂直Switch

ためandroid:rotation="90"を使用することができます

+1

静的な高さを試してみる必要があります。私は、通常のスイッチ表示で調整する必要があると仮定していました。洞察に感謝します! – RedBassett

+0

@RedBassett埋め込みが不要です。 –

+0

@RedBasset tryよりパディングを削除します –

関連する問題