TextView 没有出现在模拟器和真实设备上 [英] TextView not appearing on emulator and real device
问题描述
这是设计在预览"选项卡中的外观
但是当我在模拟器或真实设备上运行它时,有效期至没有出现.
<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.CardViewandroid:id="@+id/card_view"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_margin="@dimen/card_margin"机器人:海拔=3dp"card_view:cardCornerRadius="@dimen/sell_item_radius"><相对布局android:layout_width="match_parent"android:layout_height="match_parent"><相对布局android:layout_width="wrap_content"android:layout_height="wrap_content"机器人:layout_marginLeft="5dp"android:layout_marginTop="10dp"android:background="@android:color/holo_blue_light"><文本视图android:id="@+id/日期"android:layout_width="wrap_content"android:layout_height="wrap_content"机器人:重力=中心"android:text="@string/validUntil"android:textColor="@color/colorPrimaryDark"/><文本视图android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_toRightOf="@+id/date"机器人:文本=日期"android:textColor="@color/colorPrimaryDark"/></RelativeLayout><图像视图android:id="@+id/itemImage"android:layout_width="match_parent"android:layout_height="@dimen/sell_item_image_height"安卓:可点击=真"android:scaleType="fitXY"android:src="@drawable/ic_camera"/><图像视图android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignBottom="@+id/itemImage"android:layout_alignParentRight="true"android:src="@drawable/ic_favourite"/><文本视图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"/><文本视图android:id="@+id/标题"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:textColor="@color/blue"android:textSize="@dimen/sell_item_title"/><文本视图android:id="@+id/价格"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>
谢谢
你看不到你的一些视图的原因是你所有的视图宽度和高度都是 wrap_content
- 所以你创建了一个布局,您的视图可以根据它们的内容展开(如果您将在 600 dp 图像上添加 200 dp 并将宽度和高度设置为 wrap_content
这将是您的视图大小),您可以看到即使在您的预览中 - 您的视图也会相互重叠.
因此,您可以在视图上使用固定大小,但是这样做会进入危险区域 - 您的布局不会响应所有屏幕尺寸,这是因为不同的手机有不同的屏幕尺寸以及在一部手机上看起来不错的内容在另一部手机上看起来并不好看.
当然,您可以通过为每个屏幕尺寸创建一个单一布局来解决这个问题,但这需要大量的工作.
您还可以使用 android:weightSum
和 layout_weight
但还有更好的选择:
<小时>您可以使用
和风景:
This is how the design looked in Preview tab
But when I run it on emulator or real device, valid until date not appearing.
<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>
Thanks
The reason that you don't see some of your views is that all of your views width and height are wrap_content
- so you created a layout that your views can expand according to the content of them (if you will add 200 dp on 600 dp image and set width and height to wrap_content
this will be your view size) and you can see that even on your preview - your views are overlapping one another.
So you can either use fixed size on your views, but by doing so you are entering the danger zone - your layout won't be responsive to all screen sizes, that's because different phones got different screen size and what may look good on one phone won't look good on another phone.
Sure, you can fix this by creating a single layout for every screen size but that's a lot of work.
You can also define your relative layout with android:weightSum
and layout_weight
But there is an even better option:
You can use ConstraintLayout to achieve a single layout that is responsive to all screen sizes, all of its view hierarchy is flat (no nested view groups) and is it really simple to use.
Here is an example for a layout that looks just like you want with 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>
And here is how it looks in portrait :
And landscape:
这篇关于TextView 没有出现在模拟器和真实设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!