如何在Android中制作垂直的SeekBar?
SeekBar
可以是垂直的吗?我不太擅长UI设计,所以如何让SeekBar
更漂亮,请给我一些模板和例子。
Can a SeekBar
be vertical? I am not very good at UI design, so how can I make the SeekBar
more beautiful, please give me some templates and examples.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
尝试:
Try:
我使用了 Selva 的解决方案,但遇到了两种问题:
我解决了这两个问题。 找到解决方案(在我自己的项目包中)
您可以在https://github.com/jeisfeld/Augendiagnose/blob/master/AugendiagnoseIdea/augendiagnoseLib/src/main/java/de/jeisfeld/augendiagnoselib/components/VerticalSeekBar.java< /a>
I used Selva's solution but had two kinds of issues:
I fixed these two issues. You can find the solution (within my own project package) at
https://github.com/jeisfeld/Augendiagnose/blob/master/AugendiagnoseIdea/augendiagnoseLib/src/main/java/de/jeisfeld/augendiagnoselib/components/VerticalSeekBar.java
我们使用
android:rotation="270"
制作了一个垂直的 SeekBar:相机曝光补偿的屏幕截图:
We made a vertical SeekBar by using
android:rotation="270"
:Screenshot for camera exposure compensation:
这对我有用,只需将其放入您想要的任何布局中即可。
This worked for me, just put it into any layout you want to.
请注意,在我看来,如果更改宽度,拇指宽度不会正确更改。
我没有花时间去修复它,我只是针对我的情况修复了它。这就是我所做的。
无法弄清楚如何联系原始创建者。
Note, it appears to me that if you change the width the thumb width does not change correctly.
I didn't take the time to fix it right, i just fixed it for my case. Here is what i did.
Couldn't figure out how to contact the original creator.
将其包装在 FrameLayout 内,这样就不存在大小问题。
Wrap it inside a FrameLayout so that there is no Size issue.
使用 EditText 移动拇指时,Vertical Seekbar setProgress 可能不起作用。以下代码可以提供帮助:
此代码段可以在此处找到:
https://stackoverflow.com/a/33064140/2447726
When moving the thumb with an EditText, the Vertical Seekbar setProgress may not work. The following code can help:
This snippet code found here:
https://stackoverflow.com/a/33064140/2447726
试试这个
Try this
我尝试了很多不同的方法,但最适合我的是。
使用 Seekbar
在 FrameLayout 内和代码中
。在Seekbar上设置Pre Draw回调,您可以在其中更改Seekbar的宽度和高度
我在 C# 中完成了这部分,所以我使用的代码是“
这里我向 PreDraw 事件添加侦听器”,当其触发时,我删除 PreDraw,以便它不会进入无限循环。
因此,当执行 Pre Draw 时,我获取 FrameLayout 的高度并将其分配给 Seekbar。并将seekbar的旋转设置为270。
由于我的搜索栏位于框架布局内部,并且其重力设置为中心。我不需要担心翻译。由于 Seekbar 始终位于框架布局的中间。
我删除EventHandler的原因是因为seekbar.RequestLayout();将使该事件再次执行。
I tried in many different ways, but the one which worked for me was.
Use Seekbar inside FrameLayout
And in Code.
Setup Pre Draw callback on Seekbar, Where you can change the Width and height of the Seekbar
I did this part in c#, so Code i used was
Here i Add listener to PreDraw Event and when its triggered, I remove the PreDraw so that it doesnt go into Infinite loop.
So when Pre Draw gets executed, I fetch the Height of FrameLayout and assign it to Seekbar. And set the rotation of seekbar to 270.
As my seekbar is inside frame Layout and its Gravity is set as Center. I dont need to worry about the Translation. As Seekbar always stay in middle of Frame Layout.
Reason i remove EventHandler is because seekbar.RequestLayout(); Will make this event to be executed again.
通过使用
RotateLayout
,拥有垂直SeekBar
变得轻而易举。只需将那个可怕的SeekBar
包裹进去,Bob 就是你的叔叔:https:// github.com/rongi/rotate-layout
By using
RotateLayout
, having verticalSeekBar
is a breeze. Just wrap that horribleSeekBar
into it and Bob is your uncle:https://github.com/rongi/rotate-layout
入门
将这些行添加到 build.gradle。
用法
Java 代码
布局 XML
注意:Android N+ 需要
android:splitTrack="false"
。Getting started
Add these lines to build.gradle.
Usage
Java code
Layout XML
NOTE:
android:splitTrack="false"
is required for Android N+.就我而言,我使用了一个普通的seekBar,只是翻转了布局。
eekbark_layout.xml - 我的布局包含我们需要使其垂直的seekbar。
Activity_main.xml
在MainActivity中我旋转seekbar_layout:
因此我们有必要的垂直seekbar:
In my case, I used an ordinary seekBar and just flipped out the layout.
seekbark_layout.xml - my layout that containts seekbar which we need to make vertical.
activity_main.xml
And in MainActivity I rotate seekbar_layout:
As a result we have necessary vertical seekbar:
你可以自己做——现在太难了。
这是我项目中的一个示例: https://github.com/AlShevelev/WizardCamera
让我们从设置(attrs.xml)。
这里有几个实用函数:
最后但并非最不重要的 - 垂直搜索栏的类:
您可以按如下所示使用它:
瞧!
You can do it by yourself - it's now so difficult.
Here is an example from my project: https://github.com/AlShevelev/WizardCamera
Let start from settings (attrs.xml).
Here is a couple of utility functions:
And at last, but not least - a class for vertical seek bar:
You can use it as shown here:
Voila!
隐藏垂直SeekBar
Hidden Vertical SeekBar
我几年前制作了垂直搜索栏库来解决这个问题,请参阅:
该库不会旋转原始搜索栏90或270度,它从头开始重写,以解决“拇指不在正确的位置”,“无法从代码设置位置”,“无法排列背景可绘制”,“触摸位置跑掉”等。有一些额外有用的功能。
I made vertical seekbar library years ago for solve this see:
This library does not rotate original seekbar 90 or 270 degree, It's rewrite from scratch for resolve "Thumb not in the right place", "Can't set position from code", "Can't line up background drawable", "Touch position run away" etc. And also have some extra useful feature.
简单的答案
不要在搜索栏内使用
android:rotation="270"
,而是在环绕它的 FrameLayout 内使用它。Simple answer
Instead of using
android:rotation="270"
inside of a seek bar, use it inside of a FrameLayout that wraps around it.对于API 11及更高版本,可以使用seekbar的XML属性(android:rotation="270")来实现垂直效果。
对于较旧的 API 级别(例如 API10),仅使用 Selva 的答案:
https://github.com/AndroSelva/Vertical-SeekBar-Android
For API 11 and later, can use seekbar's XML attributes(android:rotation="270") for vertical effect.
For older API level (ex API10), only use Selva's answer:
https://github.com/AndroSelva/Vertical-SeekBar-Android
这是垂直搜索栏的一个非常好的实现。
看看吧。
http://560b.sakura.ne.jp/android/VerticalSlidebarExample.zip
这是我自己的垂直和反向搜索栏的实现,基于 this
https://github.com/AndroSelva/Vertical-SeekBar-Android
Here is a very good implementation of vertical seekbar.
Have a look.
http://560b.sakura.ne.jp/android/VerticalSlidebarExample.zip
And Here is my own implementation for Vertical and Inverted Seekbar based on this
https://github.com/AndroSelva/Vertical-SeekBar-Android
工作示例
粘贴代码并保存。现在在您的 XML 布局中使用它:
确保创建一个包
android.widget
并在此包下创建VerticalSeekBar.java
Working example
There, paste the code and save it. Now use it in your XML layout:
Make sure to create a package
android.widget
and createVerticalSeekBar.java
under this package