如何在android中创建带有滑动控件的按钮栏?

发布于 2024-09-27 04:12:05 字数 541 浏览 1 评论 0原文

HTC sense 手机上的足迹和时钟应用程序使用底部按钮栏上的滑块控件(有关示例,请参阅此影片:https://docs.google.com/leaf?id=0B9hmEy_z2CRzOTJmYTVlNDktYjkwMS00YTM5LTk3ODQtYmIzODU4Yzk1YjA0&hl=en&authkey=CMWwwegG)。

从层次结构查看器中我可以看到他们为此使用了自定义控件。我正在寻找一种方法来达到相同的效果。我想要在应用程序底部有一个带有可移动选择器的按钮栏,我可以将其从左向右或向后拖动,从而选择不同的选项卡。

谁能提供一些关于我如何实现这一目标的提示?我可以使用哪些布局控件来实现此目的?我知道如何创建选项卡和按钮栏,但我对滑动选择器有点困惑。

感谢和亲切的问候, 伊沃

The footprints and clock app on a HTC sense phones use a slider control on the bottom buttonbar (see this movie for an example: https://docs.google.com/leaf?id=0B9hmEy_z2CRzOTJmYTVlNDktYjkwMS00YTM5LTk3ODQtYmIzODU4Yzk1YjA0&hl=en&authkey=CMWwwegG).

From the hierarchy viewer I can see that they use a custom control for this. I'm looking for a way to achieve the same effect. I want a buttonbar on the bottom of my app with a movable selector which I can drag from left to right and back, thereby selecting the different tabs.

Can anyone give some tips on how I might achieve this? Which layout controls could I use for this purpose? I know how to create the tabs and the button bar, but I'm a bit puzzled on the sliding selector.

Thanks and kind regards,
Ivo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖兔^ω^ 2024-10-04 04:12:05

要创建视图,您有两个主要选项。

一种是在 Activity 将使用的布局文件中以 XML 形式定义它。

<LinearLayout ... > <!-- or any other layout, see layout documentation in the SDK -->
  <ImageView ...    <!-- or another view as fits your needs -->
    android:id="@+id/myview"
    android:src="@drawable/myimage" ... />
</LinearLayout>

然后,您可以在调用 setContentView 后使用 findViewById(R.id.myview) 在代码中引用您的视图。 (当然,如果您在 XML 文件中说“@+foo/bar”,您将使用 R.foo.bar)。

另一种方法是动态创建它并将其附加到视图层次结构。

ImageView v = new ImageView();
v.setImageResource(R.drawable.myimage);
... // initialize your view here
parent.addChild(v);

...或者这样。

要获取事件,您又有多种选择。
其中之一是子类化 View(或 ImageView)并实现 onTouchEvent。您需要调整上面的代码来创建新类的实例而不是 ImageView (在 Java 代码中很明显,对于 XML,您将编写为标记名称,并确保实现采用 (Context, AttributeSet) 的构造函数调用其超类的构造函数)。
或者,您可以编写一个 TouchDelegate 并在 ImageView 上使用 setTouchDelegate,这对于您的情况可能更简单。

另一种方法是创建一个 GestureDetector 并使用其 hitTest 方法将触摸与您的视图进行匹配。对于您所描述的任务来说,它是多余的,但当您的应用程序变得更加复杂时,它可能会派上用场。

To create a view, you have two main options.

One is to define it in XML, in a layout file your Activity will use.

<LinearLayout ... > <!-- or any other layout, see layout documentation in the SDK -->
  <ImageView ...    <!-- or another view as fits your needs -->
    android:id="@+id/myview"
    android:src="@drawable/myimage" ... />
</LinearLayout>

You can then reference your view in your code, after you called setContentView, with findViewById(R.id.myview). (of course, if you said, say, "@+foo/bar" in the XML file, you'd use R.foo.bar).

The other way is to create it dynamically and attach it to the view hierarchy.

ImageView v = new ImageView();
v.setImageResource(R.drawable.myimage);
... // initialize your view here
parent.addChild(v);

...Or so.

To get events, you have several options again.
One of them is to subclass View (or ImageView for that matter) and implement onTouchEvent. You'd need to adapt the code above to create an instance of your new class instead of an ImageView (obvious in Java code, for XML you'd write as tag name and make sure you implement the constructor that takes (Context, AttributeSet) to call its superclass' constructor).
Or, you could write a TouchDelegate and use setTouchDelegate on your ImageView, which is probably simpler in your case.

Another approach would be to create a GestureDetector and to match touches to your view using its hitTest method. For the task you are describing it's overkill, but it might come in handy when your application becomes more complicated.

扶醉桌前 2024-10-04 04:12:05

我将扩展 TabWidget 类来进行我自己的渲染。您将受益于与其他选项卡系统的热插拔和统一的界面,同时仍然能够按照您想要的方式绘制选项卡栏。
您将在 setCurrentTab 中开始动画。

I would extend the TabWidget class to do my own rendering. You'd have the benefits of hot-swappability with other tab systems and uniform interface, while still being able to draw your tab bar the way you want.
You'd start your animation in setCurrentTab.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文