ImageView无法在设备中正确显示 [英] ImageView not displaying correctly in the device
问题描述
我有这个布局,我想用作导航栏的标题:
I have this layout that I want to use as a header for navigation bar:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="164dp">
<ImageView
android:id="@+id/app_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:adjustViewBounds="false"
android:scaleType="center"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@mipmap/ic_launcher" />
<TextView
android:id="@+id/first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:text="Name"
android:textAppearance="@style/TextAppearance.AppCompat.Headline"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/app_icon" />
<ImageView
android:id="@+id/credits_material_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:scaleType="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:srcCompat="@drawable/ic_dollar_coin_stack" />
<TextView
android:id="@+id/credits_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:text="Credits"
android:textStyle="italic"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/credits_material_icon" />
<TextView
android:id="@+id/credits_available"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/credits_label" />
</android.support.constraint.ConstraintLayout>
在布局编辑器中,布局如下:
In the layout editor, the layout looks like:
但在设备上,我得到的结果如下:
But on a device, I am getting something like this:
圈出的部分是受关注的布局。在此之下,您可以看到 ListView
,但这是 LinearLayout
的子项,因此这是一个单独的故事。
The encircled portion is the layout under concern. Under that, you can see a ListView
but that is a child of LinearLayout
so that is a separate story.
所以,我的问题是为什么我的 ImageView
都没有显示?
So, my question is that why none of my ImageView
s are displaying at all?
我尝试过的事情:
- 我试过移动
ImageView
周围没有任何改进。 - 我使用布局检查器检查了布局层次结构。事实证明,它显示了一个文字点代替
ImageView
s。 - 我试图手动设置此布局的高度如果有的话,解决任何与住宿有关的问题。
- I have tried moving the
ImageView
s around with no improvement. - I checked with the layout hierarchy with the Layout inspector. Turns out, it shows a literal dot in place of both the
ImageView
s. - I have tried manually setting the height of this layout to a very large number to solve any accommodation related problems if there are any.
推荐答案
答案很简单:
我认为Android docs 建议反对它的使用一般需要认真对待。
I thought that Android docs advising against its use in general was something to be taken seriously.
这将自动成为在
布局中使用ImageView
时使用。在编写
自定义视图时,您只需要手动使用此类。
This will automatically be used when you use
ImageView
in your layouts. You should only need to manually use this class when writing custom views.
我读到的另一个地方你应该从 AppCompatActivity
扩展以启用此行为。我的活动从 AppCompatActivity
延伸,但我仍然花了很多时间思考,毕竟可能有些东西出现了 ConstraintLayout
。
Another place I read that you are supposed to be extending from AppCompatActivity
to enable this behavior. My Activities DO extend from AppCompatActivity
but still I spent so much time thinking that maybe something is buggy with ConstraintLayout
after all.
不。我的心爱的 ConstraintLayout
尚未失去其骑士精神。 :)
NO. My beloved ConstraintLayout
hasn't lost its chivalry yet. :)
所以,如果有疑问,我重申,
So, when in doubt, I reiterate,
其他一些答案说同样的话:
Some other answers saying the same thing:
这篇关于ImageView无法在设备中正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!