文本视图未显示在Android模拟器内部的滚动视图中

时间:2020-05-05 11:49:26

标签: android xml kotlin

我创建了一个简单的应用程序,该应用程序在滚动视图中显示用户名及其生物。在初始阶段,当我刚刚在Scroll View中添加一个TextView,一个ImageView和一个TextView时。但是,当我运行该应用程序时。它只是显示了最上面的TextView以及刚刚添加的星形ImageView,而不显示了ScrollView的内容。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rootLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingStart="@dimen/padding"
    android:paddingEnd="@dimen/padding">

    <TextView
        android:id="@+id/nameTextView"
        style="@style/nameStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/shivam_jha" />

    <ImageView
        android:id="@+id/starImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/layout_margin"
        android:contentDescription="@string/star"
        tools:srcCompat="@android:drawable/btn_star_big_on" />

    <ScrollView
        android:id="@+id/bioScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/layout_margin">

        <TextView
            android:id="@+id/bioTextView"
            style="@style/nameStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.1"
            tools:text="@string/test" />
    </ScrollView>
</LinearLayout>

这是字符串资源文件:

<resources>
    <string name="app_name">About Me App</string>
    <string name="shivam_jha">Shivam Jha</string>
    <string name="star">star</string>
    <string name="test">Hey, this is demo string</string>


</resources>

这是维度资源文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="text_size">20sp</dimen>
    <dimen name="small_padding">8dp</dimen>
    <dimen name="layout_margin">16dp</dimen>
    <dimen name="padding">6dp</dimen>
</resources>

这是样式资源文件:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="nameStyle">
        <item name="android:layout_marginTop">@dimen/layout_margin</item>
        <item name="android:fontFamily">@font/roboto</item>
        <item name="android:paddingTop">@dimen/small_padding</item>
        <item name="android:textColor">@android:color/black</item>
        <item name="android:textSize">@dimen/text_size</item>
    </style>

</resources>

Emulator Screen After App Run

3 个答案:

答案 0 :(得分:0)

您可以尝试在Scroll视图中使用LinearLayout,并在LinearLayout中使用TextView。 像这样:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/bioTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.1"
            style="@style/nameStyle"
            tools:text="123" />
</LinearLayout>

尝试此解决方案。

答案 1 :(得分:0)

您仍在使用LinearLayout。我建议您转到约束布局。

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/textView15"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="32dp"
    android:text="Your name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ScrollView
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="32dp"
    android:layout_marginTop="32dp"
    android:layout_marginEnd="32dp"
    android:layout_marginBottom="32dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView15">

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

        <TextView
            android:id="@+id/textView18"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="t vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat" />
    </LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

答案 2 :(得分:0)

好吧,在搜索代码一段时间之后,我知道要在Scroll View中显示文本,我使用的是tool命名空间而不是apptool名称空间仅用于伪数据:在编译时会自动删除的数据。

因此,我删除了以下行:

tool:text="@string/bio" />

使用:

android:text="@string/bio" />

现在,一切正常! 感谢Nikhil和potter soln的帮助:)

Screenshot of final result

相关问题