我是 Android 开发的新手。我目前正在开发一个应用程序,我需要在屏幕上布置 4 个按钮。按钮 1 + 2 应位于第一行,而 3 + 4 应位于第二行。我希望每个按钮的高度和宽度都相同。由于有多种屏幕尺寸,我希望按钮宽度为屏幕宽度的 40%。 是否可以仅通过布局来制作这样的东西,还是我需要在代码中计算所有内容?
注意:我想将应用程序部署到运行 Android 2.2 或更高版本的设备。
这是一个示例图形。
编辑:对于所有正在寻找方形东西的人。这是解决方案:http://android-layouts.com/category/tags/square
最佳答案
试试这个
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button3"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:text="Button" />
</LinearLayout>
</LinearLayout>
所以为了解决这个问题你必须使用android:layout_weight="40/100"="0.4"
在它之前,你必须将 android:layout_width
设置为 0。因为没有它 android:layout_weight="40/100"="0.4"
不工作。
有关 ``android:layout_weight` 的更多详细信息,请访问
What does android:layout_weight mean?
更新 1
为按钮的高度做这个任务尝试下面的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="0.6"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="0.4"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="0.6"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="0.4"
android:orientation="horizontal" >
<Button
android:id="@+id/button3"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="0.4"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="0.6"
android:text="Button" />
</LinearLayout>
</LinearLayout>
关于android - 在 Android 中布局 4 个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16884022/