Set the height of imageview according to screen from the layout

大憨熊 提交于 2020-02-24 04:59:13

问题


I am making the layout and using the layout_weight and weight_sum. I am making the orientation of linear layout to horizontal so i am able to set the width of imageview 1/3 of the screen. But I dont know how to set the height of imageview to 1/3 of the screen.

Please help me to set the imageview height to 1/3 of the screen from the layout xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:weightSum="1">

    <ImageView
    android:id="@+id/savedImageView"
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_weight=".33"
    android:background="@drawable/background" />

</LinearLayout>

Thanks in advance


回答1:


if you put an outer horizontal linearlayout like this, you will both have 1/3 screen width and 1/3 screen height for your imageview. The inner linearlayout is vertical so you can just use that if you just need 1/3 screen height

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/out"
    android:weightSum="3"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:weightSum="3" >

        <ImageView
            android:id="@+id/savedImageView"
            android:layout_width="fill_parent"
            android:layout_height="0px"
            android:layout_weight="1"
            android:background="@drawable/yourdrawablename" />
    </LinearLayout>

</LinearLayout>



回答2:


Use this:

int displayWidth = getWindowManager().getDefaultDisplay().getHeight();

ImageView imageView = (ImageView)findViewById(R.id.image);

imageView.getLayoutParams().height = displayWidth / 3;



回答3:


Do u need some thing like this or please elaborate u r problem more so that i can help u

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" 
    android:weightSum="1">

    <ImageView
        android:id="@+id/savedImageView"
        android:layout_width="0dip"
        android:layout_height="414dp"
        android:layout_weight="0.52"
        android:background="@drawable/ic_launcher" />

</LinearLayout>


来源:https://stackoverflow.com/questions/17944910/set-the-height-of-imageview-according-to-screen-from-the-layout

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