android - 如何使用 6 个按钮创建布局,如 Windows 磁贴

标签 android android-layout android-intent android-listview

我正在尝试创建一个带有 6 个按钮的布局,这些按钮可以像 Windows Phone 的磁贴一样自动适应屏幕尺寸。在代码中,我动态创建了 6 个按钮,2 个用于行,但按钮应适合填充后者的屏幕大小。我该如何继续?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
        android:layout_height="0dip"
    android:orientation="horizontal"
    android:weightSum="2" >

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up" />

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
         />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
        android:layout_height="0dip"
    android:orientation="horizontal"
    android:weightSum="2" >

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
        />

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
         />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
        android:layout_height="0dip"
    android:orientation="horizontal"
    android:weightSum="2" >

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
         />

    <Button
        android:layout_width="0dip"
                android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/conv_up"
        />

</LinearLayout>

最佳答案

我会使用一个垂直的 LinearLayout 和三行相同权重的 child ,每行是一个水平的 LinearLayout 有两个相同权重的 child ,这将确保整个区域都被填满了。对于六个按钮,性能应该不是问题。

如果性能是一个问题,您可以将行设置为 RelativeLayout 并使用支柱将其分成两半并基于此定位两个子项。

当我说 strut 时,我的意思是:

<View android:id="@+id/strut"
    android:layout_width="0dp"
    android:layout_height="0dp" 
    android:layout_centerHorizontal="true"/>

更新: 由于您正在尝试使用 LinearLayout,下面是处理高度和宽度的方法:

LinearLayout 可以有:

android:layout_width="match_parent"
android:layout_height="match_parent"

三个 LinearLayout child 将有:

android:layout_width="match_parent"
android:layout_height="0dip"

Button 将具有:

android:layout_width="0dip"
android:layout_height="match_parent"

如您所见,我们为应用权重的属性设置了 0dip(如果父级是垂直方向,则在高度上,如果父级是水平方向,则在宽度上),它需要增长填补空白。

这是完整的 XML(按钮不包含可绘制对象,因此请随意添加您的):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />

        <Button
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

结果: enter image description here

关于android - 如何使用 6 个按钮创建布局,如 Windows 磁贴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16466914/

相关文章:

android - Android N 中的作业调度程序,间隔少于 15 分钟

android - 在静态快捷方式中实现共享应用工具

java - 在处理 Intent 时如何使用 putExtra/get_Extra?

android - 适用于 IOS 和 Android 的支付网关

Android Fragments - 如何保留数据?

android - Visual Studio : Rescource. ID。找不到我的按钮

java - 单击 ListItem 后如何更改 fragment 布局?

android - onConfigurationChanged 没有被第二次调用

android - 为 ListView 中的每个元素设置不同的分隔线颜色

Android:YouTubePlayer 在 childactivity 中不起作用