Android:如何在布局之外定义按钮和其他小部件?
因此,通常您会定义一个带有单个根节点和其中包含的所有按钮等的 XML 布局。但是,如果您只想在某处定义按钮,以便稍后可以引用它们的 ID 来移动它们,那么您在哪里/如何做到这一点?
我问这个问题是因为我试图在 FrameLayout 中使用“android:foreground”字段。所以我想将其指向我定义的按钮(或其他小部件)的 ID。但是我在哪里定义这个按钮(或其他小部件)?如果我只是定义它,通常它不会最终出现在我不希望它出现的地方吗?
So generally you define an XML layout with a single root node and all the buttons and such contained inside. But if you want to just define buttons somewhere so you can reference their IDs later on to move them around, where/how do you do that?
I'm asking this question because I'm trying to use the "android:foreground" field in the FrameLayout. So I want to point this to the ID of a button (or other widget) that I've defined. But where do I define this button (or other widget)? If I just defined it normally wouldn't it end up appearing somewhere where I don't want it to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
android:foreground 在布局上添加一个可绘制对象作为覆盖层。你把可绘制的东西放在 /res/drawable
至于按钮,你总是可以在 java 中编码它们
android:foreground adds a drawable over the layout as an overlay. you put the drawable in /res/drawable
As for the buttons, you can always code them in the java
只需在
res/layout
文件夹中的my_button.xml
文件中使用任何属性定义按钮即可:然后在需要时对其进行膨胀:
Just define your button in
my_button.xml
file inres/layout
folder with any attributes:And then inflate it when needed: