簡體   English   中英

如何以編程方式獲取一個ImageView的高度和寬度,並將其設置為android中的另一個ImageView

[英]How to programmatically get the height & width of one ImageView and set it to another ImageView in android

這是代碼

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@color/white" >

<ImageView
    android:id="@+id/first_imageview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dip"
    android:layout_marginTop="5dip"
    android:src="@drawable/frame" />

  <ImageView
     android:id="@+id/second_imageview"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignTop="@id/first_imageview"
     android:layout_alignLeft="@id/first_imageview"         
     android:scaleType="fitXY" />
</RelativeLayout>

現在我需要檢測first_imageview的高度和寬度,然后將其設置為second_image_view 怎么做?

你可以設置

android:layout_alignRight="@id/inside_imageview"
android:layout_alignBottom="@id/inside_imageview" 

這將確保第二個圖像視圖與第一個圖像視圖具有相同的大小。

就像是:

second_image_view.getLayoutParams().height = first_image_view.getHeight();
second_image_view.getLayoutParams().width = first_image_view.getWidth();

應該管用。

這行代碼應該這樣做:

secondImageView.setLayoutParams(new ViewGroup.LayoutParams(
                         firstImageView.getWidth(), firstImageView.getHeight()));

如果你想使用xml這樣做,你可以按照Chitranshu的回答。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM