ImageView没有在android xml中显示(相对布局)

时间:2015-06-24 07:15:40

标签: android xml relativelayout

![我已经用线性布局编码了它(下面给出的代码),但是想知道如何使用相对布局做同样的事情,或者在给定布局时这只是一个坏主意?

线性布局 -

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

   <ImageView
    android:src="@drawable/ocean"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:scaleType="centerCrop"
  />

<TextView
    android:text="You're invited!"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:textSize="45sp"
    android:paddingTop="16dp"
    android:paddingLeft="16dp"
    android:background="#009688"/>

<TextView
    android:text="Bonfire at the beach"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="@android:color/white"
    android:paddingTop="8dp"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:textSize="24sp"
    android:background="#009688"/>

] [1]使用相对布局时,我的ImageView不会出现在屏幕上:

  <RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
  >

  <ImageView
    android:src="@drawable/ocean"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:scaleType="centerCrop"
    android:id="@+id/ocean"
  />
</RelativeLayout>

4 个答案:

答案 0 :(得分:0)

android:layout_height="0dp"您的ImageView

错误

将此更改为

android:layout_height="match_parent"

android:layout_height="wrap_content"

基本上你把你的身高设置为0.所以没什么好显示的。

修改:

您也可以删除此android:layout_weight="1"。在RelativeLayout

中无需加权

答案 1 :(得分:0)

因为您使用的是android:layout_height="0dp"

请使用android:layout_height="wrap_content"

答案 2 :(得分:0)

像这样更改ImageView的高度。

<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
  >

  <ImageView
    android:src="@drawable/ocean"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:scaleType="centerCrop"
    android:id="@+id/ocean"
  />

答案 3 :(得分:0)

您只能在layout_weight中使用LinearLayout。 试试这个。

 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertizal">

 <ImageView
   android:src="@drawable/ocean"
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1"
   android:scaleType="centerCrop"
   android:id="@+id/ocean"/>