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> 
代碼
相關問題