[英]Android SeekBars cropped before rotating
我正在尝试使用Android SeekBars创建一个垂直混音器风格的界面来控制不同的音频源。 我可以旋转许多文章所描述的SeekBars,并将它们放置在正确的位置。 但是,每个SeekBar的宽度(旋转后即为高度)正被裁剪为父视图的可用宽度。 结果是,每个SeekBar会变短一些,因为从其开始位置到父项的右边缘的距离更短。 它们都应该是此图像的全部高度:
这是设置代码。 这是在父级的setLayoutParams()
因此只要调整父级的大小,SeekBars的大小就会调整:
public void setLayoutParams (ViewGroup.LayoutParams params) {
super.setLayoutParams(params);
int sliderWidth = ViewUtils.scalePixels(this.app, 24);
int sliderPadding = ViewUtils.scalePixels(this.app, 24);
int iconSize = ViewUtils.scalePixels(this.app, 24);
int iconPadding = ViewUtils.scalePixels(this.app, 8);
int nextPosition = sliderPadding / ViewUtils.scalePixels(this.app, 2);
// all the widths and heights are backwards here due to the rotation
// rotating from the center created weirdness with the positioning
this.masterSlider.setPivotX(0);
this.recordingSlider.setPivotX(0);
this.tempoSlider.setPivotX(0);
this.pitchSlider.setPivotX(0);
this.videoSlider.setPivotX(0);
this.masterSlider.setPivotY(0);
this.recordingSlider.setPivotY(0);
this.tempoSlider.setPivotY(0);
this.pitchSlider.setPivotY(0);
this.videoSlider.setPivotY(0);
this.masterSlider.setRotation(-90);
this.recordingSlider.setRotation(-90);
this.tempoSlider.setRotation(-90);
this.pitchSlider.setRotation(-90);
this.videoSlider.setRotation(-90);
ViewUtils.setRelativeFrame(this.masterSlider, nextPosition, params.height - iconSize - iconPadding, params.height - iconSize - iconPadding, sliderWidth);
ViewUtils.setRelativeFrame(this.masterIcon, nextPosition, params.height - iconSize, iconSize, iconSize);
nextPosition += sliderWidth + sliderPadding;
ViewUtils.setRelativeFrame(this.recordingSlider, nextPosition, params.height - iconSize - iconPadding, params.height - iconSize - iconPadding, sliderWidth);
ViewUtils.setRelativeFrame(this.recordingIcon, nextPosition, params.height - iconSize, iconSize, iconSize);
nextPosition += sliderWidth + sliderPadding;
ViewUtils.setRelativeFrame(this.tempoSlider, nextPosition, params.height - iconSize - iconPadding, params.height - iconSize - iconPadding, sliderWidth);
ViewUtils.setRelativeFrame(this.tempoIcon, nextPosition, params.height - iconSize, iconSize, iconSize);
nextPosition += sliderWidth + sliderPadding;
ViewUtils.setRelativeFrame(this.pitchSlider, nextPosition, params.height - iconSize - iconPadding, params.height - iconSize - iconPadding, sliderWidth);
ViewUtils.setRelativeFrame(this.pitchIcon, nextPosition, params.height - iconSize, iconSize, iconSize);
nextPosition += sliderWidth + sliderPadding;
ViewUtils.setRelativeFrame(this.videoSlider, nextPosition, params.height - iconSize - iconPadding, params.height - iconSize - iconPadding, sliderWidth);
ViewUtils.setRelativeFrame(this.videoIcon, nextPosition, params.height - iconSize, iconSize, iconSize);
}
setRelativeFrame
只是添加RelativeLayoutParams的便捷函数:
public static void setRelativeFrame(View view, int x, int y, int w, int h) {
RelativeLayout.LayoutParams layoutParams = null;
if (view.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
layoutParams = (RelativeLayout.LayoutParams)view.getLayoutParams();
}
if (layoutParams == null) {
layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
}
layoutParams.leftMargin = x;
layoutParams.topMargin = y;
layoutParams.width = w;
layoutParams.height = h;
view.setLayoutParams(layoutParams);
}
我在Android 8.1.0和5.1.1上得到了相同的结果。
父级是一个RelativeLayout,上面有setClipChildren(true)
,但是我删除了它,这没什么区别。
另一篇文章建议将setLayerType(View.LAYER_TYPE_SOFTWARE, null)
到SeekBars,但这没什么区别。
设置尺寸之前或之后旋转均无所谓。 Android仍然将宽度限制为可用空间,就好像未旋转SeekBar一样。
我尝试创建一个新视图,将SeekBar添加到视图中并旋转视图,但是随后以与SeekBar相同的方式剪切视图。 因此,这似乎是Android视图的普遍问题,并非特定于SeekBars。 但是我仍然没有通过更广泛的搜索找到解决方案。
什么将SeekBar大小限制为其父视图,我该如何解决?
我只是找到了一种可行的解决方法:我添加了一个与父视图相同宽度的内部视图,然后向其中添加了SeekBars。 内部视图是不可见的,因此您看不到它比父视图宽,但是现在SeekBars没有被剪切。 如果有更直接的解决方案,我将不予回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.