add image to my layout with size proportional to dimension of screen android

旧街凉风 提交于 2019-12-13 01:02:37

问题


In my application i have an image that should have 25% width and height of screen. I hope this is clear. This is what i'm already tried but doesn't work. Thank you.

<ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="0.25"
        android:src="@drawable/fish"
        android:scaleType="fitCenter"
        />

回答1:


  Just use this with Linear Layout

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100">




    <ImageView 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_weight="25"
       android:background="@drawable/ic_launcher"/>
</LinearLayout>



回答2:


As I understood, your need is to relate your image view dimension with the device screen.

Here is it.

@Override
protected void onResume() {
    super.onResume();
      int w= getWindowManager().getDefaultDisplay().getWidth(); 
   Log.d("Nzm", ""+w);
    ImageView img=(ImageView)findViewById(R.id.imageview1);
   img.setWidth((int)(w/4));// to set 25%
}

Also, make sure inside layout, the parent width is fillparent, and set the id for imageview.




回答3:


try this

android:layout_width="wrap_content"


来源:https://stackoverflow.com/questions/18354000/add-image-to-my-layout-with-size-proportional-to-dimension-of-screen-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!