Android TableLayout 表示为 LinearLayout 的百分比

标签 android android-layout

我试图将 TableLayout 作为 LinearLayout 中的唯一项目(它有一个我想覆盖整个屏幕的背景图标),我想将它居中并说使用 60% 的宽度和 70% 的例如高度。我有这个,它看起来不错,但我不确定它是否适用于许多设备,并且想知道最好、最通用的表达方式是什么。

<LinearLayout
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/mylogo"
    android:padding="0dip"
    android:gravity="center_vertical|center_horizontal"
    android:baselineAligned="false"
    >

    <TableLayout
        android:background="#FFFF0000"
        android:layout_width="200dp"
        android:layout_height="250dp"
        android:gravity="center_vertical|center_horizontal"
        android:stretchColumns="field">

    </TableLayout>

</LinearLayout>

我弄乱了 layout_Weights,但我唯一能做的就是让表格消失。

谢谢 斯蒂芬

最佳答案

LinearLayout 只能通过一个维度的权重调整子级大小,但由于您真正担心的只是调整单个 View 的大小,因此您可以通过嵌套 LinearLayout 来完成此操作.

<LinearLayout
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/mylogo"
    android:gravity="center"
    android:weightSum="1.0">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:gravity="center"
        android:weightSum="1.0"
        android:layout_weight="0.7">

        <TableLayout
            android:background="#FFFF0000"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:stretchColumns="field"
            android:layout_weight="0.6">

        </TableLayout>

    </LinearLayout>
</LinearLayout>

此布局在不同设备上的显示效果是否良好取决于您的内容。总的来说,我倾向于合理的填充/边距,但这同样非常依赖于内容。

关于Android TableLayout 表示为 LinearLayout 的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9523436/

相关文章:

带有 inputType 和 Enter 按钮的 Android EditText

android - startPreview 失败但不是所有设备

Android:Repository/ViewModel 中的业务逻辑转换

android - 有没有办法将搜索栏添加到我的实际首选项屏幕?

java - 单击按钮时如何进行随机布局

javascript - 我无法在 cordova-sqlite-storage 中为 ionic 1 打开数据库

android - Cordova 调试 : the specified file was not found

android - 渲染问题无法在当前主题中找到样式 'bottomNavigationStyle'

android - AmazingListView 适配器未下载?

android - 在 Android 中自动捕获图像