【发布时间】:2013-11-28 16:59:42
【问题描述】:
从 Android 开发开始,我想知道如何在 XML 中进行此布局(使用 Android Studio),针对 4.x Android 设备。
如何在位图背景上精确定位 4 个按钮?尽管位图缩放应该可以工作。
场景: 我的应用程序主屏幕应包含 4 个放置在位图上的按钮。按钮的位置应该总是像这样,并且整体应该按比例放大/缩小(比例宽度/高度)以匹配屏幕空间。布局草图:
尝试过的解决方案:
-
带有背景的 GridLayout 中的 4 个按钮:
<GridLayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_centerHorizontal="true" android:orientation="horizontal" android:columnCount="2" android:rowCount="2"> <Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button1" /> <Button android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button2" /> <Button android:id="@+id/button3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button3" /> <Button android:id="@+id/button4" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button4" /> </GridLayout> GridLayout 中的 4 个按钮,位于 RelativeLayout 中的 ImageView 上方。
- 一个带有 onTouch() 的 ImageView,用于检查颜色编码的位图。 (source here)
关于缩放的唯一可行的解决方案是 onTouch(),但我宁愿继续使用实际按钮,以便键盘导航和按下/悬停效果按预期工作。
如何定位按钮,让它们始终与位图匹配比例? 感谢您提供任何见解。
由于标题,这个问题看起来像个骗子,但我还没有找到任何可以证明如何完成的问题。
【问题讨论】:
-
为什么不尝试相对布局而不是网格布局?
标签: android android-layout android-studio