Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试创建一个计算器应用程序,但我一直遇到 xml 布局问题。
由于按钮的数量,即使我使用 dp 来确定屏幕之间每个按钮的大小,结果也会有所不同......
我是否应该在 java 中创建布局,以便获得屏幕尺寸?如果可以,我可以举个例子吗?
您可以设计布局文件,然后使用不同大小的按钮制作几个版本。将它们放在 res/layout-small/、res/layout-large/ 和 res/layout-normal/ 中。系统会根据设备的显示尺寸拉取合适的 xml 文件。
另一种选择是尝试通过在 dp 中指定大小来设计布局,而不是使用android:layout_weight并android:weightSum实现您想要的比例。然后它应该根据设备自行扩展,而不需要任何不同的 xml 文件。
android:layout_weight
android:weightSum