如何使滚动视图正确显示在屏幕上

问题描述 投票:0回答:1

我对滚动视图有疑问。我在互联网上查询,但找不到适合的解决方案。我在应用程序中使用窗口活动。在添加scrollview之前,我可以毫无问题地使用它。 Scrollview不适用于屏幕。

这是我的活动代码。

class ExtendedMedicineInfoActivity : Activity() {
private lateinit var binding: ActivityExtendedMedicineInfoBinding
var medicineUsageData = MedicineUsageData()
var gson = Gson()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityExtendedMedicineInfoBinding.inflate(layoutInflater)
    val view = binding.root
    setContentView(view)

    medicineUsageData = gson.fromJson(intent.getStringExtra("medicineData"), MedicineUsageData::class.java)

    val displayMetrics = DisplayMetrics()
    windowManager.defaultDisplay.getMetrics(displayMetrics)
    val width = displayMetrics.widthPixels
    val height = displayMetrics.heightPixels
    window.setLayout((width * .8).toInt(), (height * .8).toInt())
    val params = window.attributes
    params.gravity = Gravity.CENTER
    params.x = 0
    params.y = -20
    window.attributes = params
    setFields()
}

private fun setFields() {
    binding.expireDateEditText.setText(medicineUsageData.expireDate)
    binding.barcodeNumEditText.setText(medicineUsageData.barcodeNum)
    binding.companyNameEditText.setText(medicineUsageData.companyName)
    binding.drugSubstanceEditText.setText(medicineUsageData.drugSubstance)
    binding.medicineNameEditText.setText(medicineUsageData.productName)
    binding.partyNumEditText.setText(medicineUsageData.partyNum)
    binding.serialNumEditText.setText(medicineUsageData.serialNum)
}
}

此代码不会产生任何错误。我也将其用于其他窗口活动。但这是我第一次尝试向其中添加滚动视图。

这里是此活动的xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:gravity="center"
    android:orientation="vertical"
    android:background="@drawable/popup_medicine_list"
    tools:context=".ExtendedMedicineInfoActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:text="@string/medicine_information"
        android:textSize="32sp" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

    <GridLayout
        android:id="@+id/grid_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:clipToPadding="false"

        android:columnCount="1"
        android:padding="16dp">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/medicine_name_text_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:hint="@string/medicine_name">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/medicine_name_edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:cursorVisible="false"
                android:textColor="@color/black" />
        </com.google.android.material.textfield.TextInputLayout>

        <Space />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/expire_date_text_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:hint="@string/expire_date">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/expire_date_edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:cursorVisible="false"
                android:textColor="@color/black" />
        </com.google.android.material.textfield.TextInputLayout>

        <Space />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/company_name_text_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:hint="@string/company_name">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/company_name_edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:cursorVisible="false"
                android:textColor="@color/black" />
        </com.google.android.material.textfield.TextInputLayout>

        <Space />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/barcode_num_text_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:hint="@string/barcode_number">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/barcode_num_edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:cursorVisible="false"
                android:textColor="@color/black" />
        </com.google.android.material.textfield.TextInputLayout>

        <Space />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/drug_substance_text_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:hint="@string/drug_substance">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/drug_substance_edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:cursorVisible="false"
                android:textColor="@color/black" />
        </com.google.android.material.textfield.TextInputLayout>

        <Space />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/party_num_text_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:hint="@string/party_number">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/party_num_edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:cursorVisible="false"
                android:textColor="@color/black" />
        </com.google.android.material.textfield.TextInputLayout>

        <Space />

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/serial_num_text_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:hint="@string/serial_number">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/serial_num_edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:focusable="false"
                android:cursorVisible="false"
                android:textColor="@color/black" />
        </com.google.android.material.textfield.TextInputLayout>

    </GridLayout>

    </ScrollView>

    <com.google.android.material.button.MaterialButton
        android:id="@+id/delete_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/delete" />

</LinearLayout>

此代码构建并正常运行。但这是结果。

<<

您可以看到,顶部还有另一个元素,但是没有显示。

<<

并且下面有多余的空间。

我可以通过在内部GridLayout中添加android:layout_marginTop="56dp"来解决较大的问题。但是我知道这不是最好的解决方案。屏幕尺寸可以改变或其他。即使我添加此行,也无法解决我的下行冗余空间问题。

任何人都可以帮助我解决这个问题。预先感谢。

我对滚动视图有疑问。我在互联网上查询,但找不到适合的解决方案。我在应用程序中使用窗口活动。直到我添加...

android android-studio kotlin scrollview
1个回答
0
投票
这是我更改的代码。
© www.soinside.com 2019 - 2024. All rights reserved.