2017-12-19 5 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.

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 뷰 시리즈 다음 -90android:rotation 매개로 통상 Switch 다음 결과 (제 1 스위치)이며

draw 뷰는 일반적으로 수평 스위치의 원래 치수를 유지하는 범위 바깥으로 확장되므로 회전이있는 보통 Switch이 시각적으로 잘리는 것을 볼 수 있습니다. 그러나 사용자 정의 VerticalSwitch은 올바른 높이 (두 번째 스위치가 전체 드로어 블을 표시 할 수 있음)를 가지지 만 드로어 블은 뷰의 아래쪽 반쪽으로 오프셋되고 드로어 블은 여전히 ​​뷰의 아래쪽이 잘린 부분 아래로 잘립니다 수평 구성에서.

디버거에서 크기 조정 매개 변수를 검사하면 회전 된 새 치수가 올바르게 적용되지만 클리핑이 여전히 발생하고 있음을 알 수 있습니다. 오프셋과 클리핑의 원인은 무엇이며 어떻게 수정 될 수 있습니까?

답변

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"을 사용할 수 있습니다 Switch 수직 정의를 만들 필요가 없습니다

+1

정적 높이를 시도해야합니다. 정상적인 스위치보기에서 패딩을 조정해야한다고 생각했습니다. 통찰력에 감사드립니다! (사용자 정의보기에서 클리핑을 수정하기위한 질문이 아직 열리지 않았습니다.) – RedBassett

+0

@RedBassett 패딩이 필요 없음 –

+0

@RedBasset 시도보다 패딩 제거 –

관련 문제