【问题标题】:TextView not appearing on emulator and real deviceTextView 没有出现在模拟器和真实设备上
【发布时间】:2019-07-31 04:12:04
【问题描述】:

这就是设计在“预览”选项卡中的外观

但是当我在模拟器或真实设备上运行它时,有效期至日期没有出现。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/card_margin"
        android:elevation="3dp"
        card_view:cardCornerRadius="@dimen/sell_item_radius">

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

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/holo_blue_light">

                <TextView
                    android:id="@+id/date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/validUntil"
                    android:textColor="@color/colorPrimaryDark" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_toRightOf="@+id/date"
                    android:text="date"
                    android:textColor="@color/colorPrimaryDark" />

            </RelativeLayout>

            <ImageView
                android:id="@+id/itemImage"
                android:layout_width="match_parent"
                android:layout_height="@dimen/sell_item_image_height"
                android:clickable="true"
                android:scaleType="fitXY"
                android:src="@drawable/ic_camera" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/itemImage"
                android:layout_alignParentRight="true"
                android:src="@drawable/ic_favourite" />

            <TextView
                android:id="@+id/imageCount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginTop="5dp"
                android:layout_marginRight="10dp"
                android:text="@string/imageCount"
                android:textColor="@color/blue" />

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/itemImage"
                android:layout_marginTop="-3dp"
                android:paddingLeft="@dimen/sell_item_image_padding"
                android:paddingTop="@dimen/sell_item_image_padding"
                android:paddingRight="@dimen/sell_item_image_padding"
                android:text="@string/title"
                android:textColor="@color/blue"
                android:textSize="@dimen/sell_item_title" />

            <TextView
                android:id="@+id/price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:layout_marginRight="10dp"
                android:paddingLeft="@dimen/sell_item_image_padding"
                android:paddingRight="@dimen/sell_item_image_padding"
                android:text="@string/price" />
        </RelativeLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

谢谢

【问题讨论】:

    标签: android xml android-layout


    【解决方案1】:

    您看不到某些视图的原因是您所有视图的宽度和高度都是wrap_content - 所以您创建了一个布局,您的视图可以根据它们的内容展开(如果您要添加在 600 dp 图像上设置 200 dp 并将宽度和高度设置为 wrap_content 这将是您的视图大小),即使在预览中您也可以看到 - 您的视图相互重叠。

    因此,您可以在视图上使用固定尺寸,但是这样做会进入危险区域 - 您的布局不会响应所有屏幕尺寸,这是因为不同的手机有不同的屏幕尺寸以及在一部手机上看起来不错的东西在另一部手机上看起来并不好。


    当然,您可以通过为每种屏幕尺寸创建一个布局来解决此问题,但这需要大量工作。

    您还可以使用android:weightSumlayout_weight 定义您的相对布局

    但还有更好的选择:


    你可以使用ConstraintLayout 来实现一个单一的布局,它可以响应所有的屏幕尺寸,它的所有视图层次都是扁平的(没有嵌套的视图组)并且使用起来真的很简单。

    这是一个布局示例,看起来就像你想要的约束布局:

    <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/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/textView12"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
    
    <TextView
        android:id="@+id/textView13"
        android:layout_width="0dp"
        android:layout_height="15dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/textView12"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
    
    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="TextView"
        app:layout_constraintStart_toEndOf="@+id/textView10"
        app:layout_constraintTop_toTopOf="parent" />
    
    <TextView
        android:id="@+id/textView14"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="heart"
        app:layout_constraintBottom_toTopOf="@+id/textView13"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/textView8" />
    
    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toTopOf="@+id/textView14"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView8"
        tools:src="@tools:sample/avatars[11]" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    这是它的纵向外观:

    和风景:

    【讨论】:

    • 谢谢你的回答,但我想在cardView
    • 您可以用 cardview 替换/包装我的示例中的视图,这只是一个示例。随意根据您的需要更改它
    • 我终于解决了!我只是移动imageView(id itemImage),将它放在第一个RelativeLayout之后
    • 再说一次,这可能适用于单个手机,但不适用于其他手机
    【解决方案2】:

    终于解决了!我只是移动带有 id itemImageimageView,将它放在第一个 RelativeLayout 之后。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-07
      • 2015-12-25
      • 2012-11-27
      • 2010-11-10
      • 1970-01-01
      • 2021-04-17
      • 2013-06-30
      • 1970-01-01
      相关资源
      最近更新 更多