0

我有两个imageView大小的相关布局。这两个图像将以编程方式加载,我想将relativelayout的大小设置为等于imgBackground的大小。我怎样才能做到这一点?我尝试了以下,但没有奏效。布局看起来比imgBackground大得多。Android如何以编程方式设置相同大小的图像视图的布局

imageLayout.getLayoutParams().width = imgBackground.getWidth(); 
imageLayout.getLayoutParams().height = imgBackground.getHeight(); 

下面是布局

<RelativeLayout 
    android:id="@+id/imageLayout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@color/black"> 
    <ImageView 
     android:id="@+id/imgPhoto" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
    /> 

    <ImageView 
     android:id="@+id/imgBackground" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
    /> 
</RelativeLayout> 

回答

0
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout 
     android:id="@+id/imageLayout" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true"> 

     <ImageView 
      android:id="@+id/imgBackground" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <ImageView 
      android:id="@+id/imgPhoto" 
      android:layout_alignBottom="@id/imgBackground" 
      android:layout_alignLeft="@id/imgBackground" 
      android:layout_alignRight="@id/imgBackground" 
      android:layout_alignTop="@id/imgBackground" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:scaleType="center" /> 

    </RelativeLayout> 
</RelativeLayout> 
代码
相关问题