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
뷰 시리즈 다음
-90
의
android:rotation
매개로 통상
Switch
다음 결과 (제 1 스위치)이며
draw 뷰는 일반적으로 수평 스위치의 원래 치수를 유지하는 범위 바깥으로 확장되므로 회전이있는 보통 Switch
이 시각적으로 잘리는 것을 볼 수 있습니다. 그러나 사용자 정의 VerticalSwitch
은 올바른 높이 (두 번째 스위치가 전체 드로어 블을 표시 할 수 있음)를 가지지 만 드로어 블은 뷰의 아래쪽 반쪽으로 오프셋되고 드로어 블은 여전히 뷰의 아래쪽이 잘린 부분 아래로 잘립니다 수평 구성에서.
디버거에서 크기 조정 매개 변수를 검사하면 회전 된 새 치수가 올바르게 적용되지만 클리핑이 여전히 발생하고 있음을 알 수 있습니다. 오프셋과 클리핑의 원인은 무엇이며 어떻게 수정 될 수 있습니까?
정적 높이를 시도해야합니다. 정상적인 스위치보기에서 패딩을 조정해야한다고 생각했습니다. 통찰력에 감사드립니다! (사용자 정의보기에서 클리핑을 수정하기위한 질문이 아직 열리지 않았습니다.) – RedBassett
@RedBassett 패딩이 필요 없음 –
@RedBasset 시도보다 패딩 제거 –