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
、続いて、結果(第一のスイッチ)である。
ドローイングビューの境界から、回転を伴う通常のSwitch
は、通常、水平スイッチの元の寸法を保持している境界の外にドロアブルが伸びるため、視覚的にクリップされることがわかります。ただし、カスタムVerticalSwitch
は正しい高さを持っています(2番目のスイッチで完全なドロアブルが表示されます)。ただし、ドロアブルはビューの下半分にオフセットされています。水平な構成である。
デバッガでサイジングのパラメータを調べると、回転した新しい寸法が正しく適用されていることがわかりますが、クリッピングはまだ発生しています。オフセットとクリッピングの原因は何ですか?また、どのように修正できますか?垂直カスタムSwitch
を作成する
静的な高さを試してみる必要があります。私は、通常のスイッチ表示で調整する必要があると仮定していました。洞察に感謝します! – RedBassett
@RedBassett埋め込みが不要です。 –
@RedBasset tryよりパディングを削除します –