【问题标题】:Android how to set the layout the same size of an imageview programmaticallyAndroid如何以编程方式将布局设置为与imageview相同的大小
【发布时间】:2017-09-17 17:19:48
【问题描述】:

我有两个 imageView 大小的相对布局。两个图像都将以编程方式加载,我想将 relativelayout 的大小设置为等于 imgBackground 的大小。我怎样才能做到这一点?我尝试了以下方法,但没有成功。布局似乎比 imgBackground 大得多。

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>

【问题讨论】:

    标签: android android-layout android-imageview android-image android-image-capture


    【解决方案1】:
    <?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>
    

    【讨论】:

      猜你喜欢
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-13
      • 1970-01-01
      相关资源
      最近更新 更多