【问题标题】:Layout issue with ScrollView & tableScrollView 和表格的布局问题
【发布时间】:2017-10-15 18:18:24
【问题描述】:

我有一个使用 ScrollView 的表单。表单中有 2 个微调器,在纵向模式下,其中一个微调器会离开屏幕,但在横向视图中,微调器是可见的。需要更改什么设置以使微调器不会离开屏幕? (注释-图片中的Select Month标签实际上应该是“Select Month & Year”)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp">
<!-- android:layout_width="wrap_content"
    android:layout_height="match_parent"
-->

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/back"
        android:scrollbars="vertical"
        android:textSize="20sp">
        <!--Month Combo Box-->
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Select Month & Year"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <Spinner
                android:id="@+id/spinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <Spinner
                android:id="@+id/spinneryr"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />
        </TableRow>
        <!--Field1 Combo Box-->
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/ERYTHROCYTE"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />


            <EditText
                android:id="@+id/ERYTHROCYTE"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back"

                android:inputType="numberDecimal" />
        </TableRow>
        <!--Field2 Combo Box-->
        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/HAEMOGLOBIN"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/HAEMOGLOBIN"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back"

                android:inputType="numberDecimal" />
        </TableRow>
        <!--Field3 Combo Box-->
        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/PCV"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/PCV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back"

                android:inputType="numberDecimal" />
        </TableRow>
        <!--Field4 Combo Box-->
        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/RDW"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/RDW"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back"

                android:inputType="numberDecimal" />
        </TableRow>
        <!--Field5 Combo Box-->
        <TableRow
            android:id="@+id/tableRow6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/MCV"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/MCV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back"
                android:gravity="center"
                android:inputType="numberDecimal" />
        </TableRow>
        <!--Field6 Combo Box-->
        <TableRow
            android:id="@+id/tableRow7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/MCH"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/MCH"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back"

                android:inputType="numberDecimal" />
        </TableRow>

        <!--Field7 Combo Box-->
        <TableRow
            android:id="@+id/tableRow8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/MCHC"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/MCHC"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back"
                android:gravity="center"
                android:inputType="numberDecimal" />
        </TableRow>
        <!--Field8 Combo Box-->
        <TableRow
            android:id="@+id/tableRow9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/LEUCOCYTECOUNT"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/LEUCOCYTECOUNT"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back"
                android:gravity="center"
                android:inputType="numberDecimal" />
        </TableRow>
        <!--Field9 Combo Box-->
        <TableRow
            android:id="@+id/tableRow10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/PLATELETS"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/PLATELETS"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back"
                android:gravity="center"
                android:inputType="numberDecimal" />
        </TableRow>
        <!--Field10 Combo Box-->
        <TableRow
            android:id="@+id/tableRow11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/NEUTROPHILS"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/NEUTROPHILS"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back"
                android:gravity="center"
                android:inputType="numberDecimal" />
        </TableRow>
        <!--Field11 Combo Box-->
        <TableRow
            android:id="@+id/tableRow12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/EOSINOPHILS"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/EOSINOPHILS"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back"
                android:gravity="center"
                android:inputType="numberDecimal" />
        </TableRow>
        <!--Field12 Combo Box-->

        <TableRow
            android:id="@+id/tableRow13"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="20sp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/LYMPHOCYTES"
                android:textColor="#0c0b0b"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/LYMPHOCYTES"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/back"
                android:gravity="center"
                android:inputType="numberDecimal" />
        </TableRow>
        <!-- Button :Start here-->
        <TableRow
            android:id="@+id/tableRow14"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="20sp">

            <Button
                android:id="@+id/btnAdd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/add" />
            <Button
                android:id="@+id/btnViewAll"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/view_all" />
        </TableRow>

            <!--<Button
                android:id="@+id/btnDelete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/delete" />

            <Button
                android:id="@+id/btnShowInfo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/show_info" />
        </TableRow>-->

<!--        <TableRow
            android:id="@+id/tableRow15"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="20sp">

            <Button
                android:id="@+id/btnModify"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/modify" />

            <Button
                android:id="@+id/btnView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/view" />




           <Button
            android:id="@+id/btnShowInfo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/show_info" >
        </TableRow-->

        <TextView
            android:id="@+id/tvValues"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textSize="20sp" />
    </TableLayout>
</ScrollView>

【问题讨论】:

  • 我接受 Marcin Orlowski 的建议,我认为这是正确的实施方式。

标签: android android-layout scrollview material-design


【解决方案1】:

需要更改什么设置以使微调器不会离开屏幕?

没有。没有任何设置 - 这基本上是您制作布局的方式,它根本不适合。您可以考虑为每个方向设置单独的布局文件,即垂直放置微调器以进行纵向布局 -> 只需在您的 res/ 中创建 layout-land 并在那里复制当前布局。然后编辑layout中的那个,让它更适合屏幕。就是这样。

【讨论】:

  • 谢谢先生。您的回答很有道理,我会实施。还有一个问题,在我重新编译应用程序以使用 SDK 25 之前,这种布局设计在 SDK 23 上运行良好。这可能还是我错了?
  • 这实际上是解决此问题的更好方法! .. 和@Ajay:这不是 abt sdk 版本,只是表格布局以这种方式工作!最好按照马文建议的方式工作。谢谢!!
【解决方案2】:

我正在发布我的问题的答案。我设法通过对 ScrollView 进行以下更改来解决此问题。更改后,TextView 和两个微调器现在在同一行上可见并且对齐良好。

Code changes to ScrollView -
    1) Updating android:layout_width="fill_parent" 
    2) Updating android:layout_height="fill_parent"
    3) Adding android:fillViewport="true"
Code changes to TableLayout 
    1) Added android:layout_centerInParent="true"
    2) Added android:gravity="center"

@Marcin 建议的解决方案是解决问题的另一种方法(可能是更通用的解决方案),但由于上述解决方案有效,我没有尝试过。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多