【问题标题】:How to make TableLayout Responsive如何使 TableLayout 响应式
【发布时间】:2017-03-21 18:18:38
【问题描述】:

目前我正在开发一些包含listandroid 应用程序。 我在移动(手机)设备中制作了一个。 我想在平板电脑(7“-10”)显示器中制作responsive。因此headercolumn 看起来与移动显示中的相同。

这是 5" 和 7" 显示器的区别:

这是我当前的 xml 代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:tag="reqtag"
>
<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:showDividers="beginning|end"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Date"
            android:id="@+id/tvClaimDateReimbursementRequest"

            android:layout_marginRight="50dp"
            android:textSize="12dp"
            android:width="100dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Type"
            android:textSize="12dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:id="@+id/tvTypeRequestReimbursement"
            android:width="80dp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Amount"
            android:textSize="10dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:width="100dp"
            android:id="@+id/tvAmountReimbursementRequest" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Status"
            android:textSize="12dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:id="@+id/tvStatusReimbursementRequest" />
    </TableRow>
</TableLayout>

我在这里看到了文档https://developer.android.com/guide/practices/screens_support.html

但我还是不明白, 有没有最佳实践来做这样的事情?

谢谢

【问题讨论】:

  • 更改线性布局的表格:我会添加一个父线性布局(垂直)和内部:线性布局(垂直),在这个线性布局下,recyclerview。为了让它反应灵敏,就像 Stas Melnychenko 所说的那样使用重力。删除边距,给更大的文本大小并使用strings.xml添加文本,:不要对文本进行硬编码,如果需要维护代码,它没有用。

标签: java android android-studio tablelayout


【解决方案1】:

你可以试试设置

android:layout_width="0dp"
android:layout_weight="1"

对于每个标头TextView,因为TableRowLinearLayout

但我相信TableView 不是最好的方法。如果您描述您想要实现的目标,我们可以找出更好的方法。

【讨论】:

  • 可能类似于交错布局(RecyclerView)
【解决方案2】:

为您的表格布局分配一个权重。然后将该权重平均分配给您的所有文本视图。喜欢:

<TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weight_sum="1"  >

<TextView
            android:layout_width="0dp"
            android:layout_weight=".2" // for five texts .2 for each
            android:layout_height="wrap_content" 
            ......  />

【讨论】:

    【解决方案3】:

    在您的表格行中尝试此代码 水平排列其子级的布局。 TableRow 应始终用作 TableLayout 的子项。如果 TableRow 的父级不是 TableLayout,则 TableRow 将表现为水平 LinearLayout。

    TableRow 的子项不需要在 XML 文件中指定 layout_width 和 layout_height 属性。 TableRow 始终强制这些值分别为 MATCH_PARENT 和 WRAP_CONTENT。

    【讨论】:

      【解决方案4】:

      您应该对文本使用 sp,对视图使用 dp。所以应该是android:textSize="12sp"而不是android:textSizes="12dp"

      更多详情请看这里:https://developer.android.com/guide/topics/resources/more-resources.html#Dimension

      【讨论】:

        猜你喜欢
        • 2021-07-03
        • 2016-02-09
        • 2014-06-05
        • 2016-12-22
        • 2017-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多