【问题标题】:Kotlin Layout Doesn't Fit the ScreenKotlin 布局不适合屏幕
【发布时间】:2021-09-15 21:12:26
【问题描述】:

PROBLEM IMAGE CLICK HERE

我的布局不适合屏幕我该如何解决这个问题?谢谢您的帮助。我试图显示来自ViewModel 的数据。

这是我的 XML 代码

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/detailTopText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView" />
        <ImageView
            android:id="@+id/detailImage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher_background" />
        <TextView
            android:id="@+id/detailDesText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="TextView2" />
    </LinearLayout>
</ScrollView>

【问题讨论】:

  • 问题不在于这种布局,而在于您如何设置操作栏和窗口。我们需要查看您的活动代码。
  • 这是一个片段,里面没有关于动作栏的代码

标签: java android kotlin layout scrollview


【解决方案1】:

这是你的新布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/detailTopText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <ImageView
                android:id="@+id/detailImage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="fitCenter"
                app:srcCompat="@drawable/flatwoods_img_btn_" />

            <TextView
                android:id="@+id/detailDesText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TextView2" />


        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • 谢谢,这很好,但我看不到第一个 textview 和 imageview 的顶部,你认为我的 android studio 有错误吗?
  • 从组件树中选择视图,然后在属性面板中根据需要添加约束。
  • 我将这段代码“android:paddingTop="?android:attr/actionBarSize"”添加到了约束布局,它成功了,感谢Haris Muntazir 有一个美好的一天
【解决方案2】:

尝试在线性布局中设置页边距

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:orientation="vertical">

【讨论】:

    【解决方案3】:

    您可以将带有操作栏大小的 paddingTop 或 marginTop 属性添加到ScrollView

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?android:attr/actionBarSize">
    

    【讨论】:

    • 感谢它的工作,但在我的顶部文本视图中看不到一点点
    猜你喜欢
    • 2017-07-05
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-07
    • 2020-12-06
    相关资源
    最近更新 更多