【问题标题】:TextView inside CardView isn't displayingCardView 中的 TextView 不显示
【发布时间】:2017-05-22 14:25:29
【问题描述】:

当我在 CardView 中使用 TextView 时,虽然文本显示在预览布局中,但它并未显示在移动设备中。我尝试删除其他视图和布局,但 TextView 仍然没有出现。 activity-about.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:statusBarScrim="@null"
            app:title="About">

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/beersheba"
                app:layout_collapseMode="parallax"/>


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme"/>
        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_margin="10dp">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                tools:ignore="UnusedAttribute">
                <ImageView
                    android:id="@+id/cover"
                    android:src="@drawable/cover_one"
                    android:layout_width="match_parent"
                    android:layout_height="76dp"
                    android:scaleType="centerCrop"
                    tools:background="#80000000"
                    tools:ignore="ContentDescription" />

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

                    <de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/circleImageView"
                        android:layout_marginTop="30dp"
                        android:layout_width="90dp"
                        android:layout_height="90dp"
                        android:layout_gravity="center_horizontal"
                        android:src="@drawable/testimony_one"
                        app:civ_border_color="#ffffff"
                        app:civ_border_width="4dp"/>
                    <TextView
                        android:id="@+id/title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="8dp"
                        android:paddingLeft="16dp"
                        android:paddingRight="16dp"
                        android:textColor="@color/textColorPrimary"
                        android:textSize="22sp"
                        tools:text="@string/title_one" />
                    <TextView
                        android:id="@+id/description"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:paddingLeft="16dp"
                        android:paddingRight="16dp"
                        android:textColor="@color/textColorSecondary"
                        android:textSize="16sp"
                        tools:text="@string/description_one" />

                </LinearLayout>

            </FrameLayout>
        </android.support.v7.widget.CardView>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

android studio preview

text doesn't appear on phone

【问题讨论】:

  • 可以发截图。在这些情况下,还可以尝试使用 Developer Options 中的 Show Layout Bounds 来捕捉您的视图。
  • 添加了截图

标签: android xml textview cardview


【解决方案1】:

非常愚蠢的错误

改变这个

tools:text="@string/description_one"

android:text="@string/description_one"

【讨论】:

  • 我喜欢这个 'android:text="@string/description_one"',它在 android studio 中渲染得非常好,但在实时设备上却不显示。我必须先设置 文本颜色,然后才能看到任何内容。
  • @Neri 也许你使用的是黑色背景?
【解决方案2】:

检查一下,它会在 cardView 中显示 textview。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:card_view="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:layout_margin="5dp"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="10dp">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textStyle="bold"
                android:layout_alignParentTop="true"/>

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_below="@+id/textView"/>
        </RelativeLayout>
    </android.support.v7.widget.CardView> 

【讨论】:

  • 试过了,但是应用崩溃了。
  • 什么是错误?导致问题的原因可能是您调用了一个不存在的小部件
  • 日志中没有显示错误。不,我正在使用已经存在的小部件。
  • 如果您在 logcat 下使用 Android Studio,请选择您的设备并在错误/日志级别下找出导致您的应用崩溃的原因。
【解决方案3】:

您的代码完全正确。但是您是否检查了所需的库依赖项? 您应该同时导入 CardView 和 RecyclerView 库。

使用它并同步。我认为它有效

    compile 'com.android.support:cardview-v7:+'
    compile 'com.android.support:recyclerview-v7:+'

【讨论】:

    猜你喜欢
    • 2015-10-30
    • 2017-09-14
    • 1970-01-01
    • 2018-01-18
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多