[英]How to use ImageView as RelativeLayout background?
I need to use an ImageView as a layout background, because the image will be defined dynamically in code, from url, but the ImageView doesn't fit all the layout. 我需要使用ImageView作为布局背景,因为图像将在代码中动态定义,来自url,但ImageView不适合所有布局。
I've tried to use height as fill_parent, match_parent, use scale_type="fitXY", but doesn't work. 我尝试使用height作为fill_parent,match_parent,使用scale_type =“fitXY”,但不起作用。
Here's my xml: 这是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="@+id/layout_barra_dados"
android:background="@color/com_facebook_blue">
<ImageView
android:id="@+id/img_bg_perfil"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/layout_img_perfil">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="@+id/img_perfil"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/botaorosa"
android:layout_margin="20dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layout_dados_perfil"
android:layout_toRightOf="@+id/layout_img_perfil"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="@+id/layout_nome_perfil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:layout_margin="20dp">
<TextView
android:id="@+id/txt_nome_perfil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Danoel"
android:layout_marginBottom="10dp"/>
<ImageView
android:id="@+id/img_local_perfil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_nome_perfil"
android:src="@drawable/icone_local_perfil" />
<TextView
android:id="@+id/txt_local_perfil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txt_nome_perfil"
android:layout_toRightOf="@+id/img_local_perfil"
android:text="Belo Horizonte" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layout_status"
android:layout_weight="1"
android:layout_marginLeft="20dp">
<ImageView
android:id="@+id/img_icone_feito_perfil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icone_feito_perfil"/>
<TextView
android:id="@+id/txt_feito_perfil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:layout_toRightOf="@+id/img_icone_feito_perfil"/>
<ImageView
android:id="@+id/img_icone_quero_perfil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icone_quero_perfil"
android:layout_toRightOf="@+id/txt_feito_perfil"
android:layout_marginLeft="20dp"/>
<TextView
android:id="@+id/txt_quero_perfil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
android:layout_toRightOf="@+id/img_icone_quero_perfil"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/layout_barra_seguidores"
android:layout_below="@+id/layout_barra_dados">
<RelativeLayout
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/layout_seguidores"
android:layout_weight="1"
android:background="@android:color/white">
<TextView
android:id="@+id/txt_header_seguidores"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_header_seguidores"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/txt_seguir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10k"
android:layout_below="@+id/txt_header_seguidores"
android:layout_centerHorizontal="true"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/layout_seguindo"
android:layout_weight="1"
android:background="@android:color/white">
<TextView
android:id="@+id/txt_header_seguindo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_header_seguindo"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/txt_seguindo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10k"
android:layout_below="@+id/txt_header_seguindo"
android:layout_centerHorizontal="true"
android:textStyle="bold"
/>
</RelativeLayout>
<RelativeLayout
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/layout_pontuacao"
android:layout_weight="1"
android:background="@android:color/white">
<TextView
android:id="@+id/txt_header_pontuacao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/txt_header_pontuacao"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/txt_pontuacao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10k"
android:layout_below="@+id/txt_header_pontuacao"
android:layout_centerHorizontal="true"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/layout_barra_lista"
android:layout_below="@+id/layout_barra_seguidores"
android:background="@android:color/white">
<Button
android:id="@+id/btn_seguir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/botao"
android:layout_alignParentLeft="true"
android:text="@string/btn_Seguir"
android:textColor="@android:color/white"
android:textStyle="bold" />
<LinearLayout
android:id="@+id/layout_btns"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentRight="true">
<Button
android:id="@+id/btn_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/icone_listagem01"/>
<Button
android:id="@+id/btn_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="@drawable/icone_listagem02"/>
</LinearLayout>
</RelativeLayout>
<ImageView
android:id="@+id/img_barra_seta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/barra_seta"
android:layout_below="@+id/layout_barra_lista"
android:scaleType="fitXY"/>
</RelativeLayout>
EDITED: 编辑:
Here's the code when load the image from URL 这是从URL加载图像时的代码
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(getApplicationContext()));
DisplayImageOptions options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_stub)
.showImageForEmptyUri(R.drawable.ic_empty)
.showImageOnFail(R.drawable.ic_error)
.cacheInMemory(true)
.cacheOnDisc(true)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageView imgPhotos = (ImageView) convertView.findViewById(R.id.img_photo);
imageLoader.displayImage(url, imgPhotos, options);
你可以使用android:layout_alignParentTop和android:layout_alignParentBottom,这样图像就会被拉伸。
You can either: Set the image as a background to the relative layout ( android:background="@drawable/WHATEVER"
) 您可以:将图像设置为相对布局的背景(
android:background="@drawable/WHATEVER"
)
OR 要么
Move the ImageView to the BOTTOM of your code, instead or below 将ImageView移动到代码的BOTTOM,代替或低于
<ImageView
android:id="@+id/img_barra_seta"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.