根据布局从屏幕设置imageview的高度

时间:2013-07-30 10:46:24

标签: android android-layout

我正在制作布局并使用layout_weight和weight_sum。我正在将线性布局的方向设置为水平,这样我就可以设置屏幕的1/3视图宽度。但我不知道如何将imageview的高度设置为屏幕的1/3。

请帮我将imageview高度设置为布局xml的屏幕的1/3。

<?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>

提前致谢

3 个答案:

答案 0 :(得分:14)

使用此:

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

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

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

答案 1 :(得分:11)

如果你设置一个像这样的外部水平线性布局,你的图像视图将有1/3的屏幕宽度和1/3的屏幕高度。内部线性布局是垂直的,所以如果你只需要1/3的屏幕高度,你可以使用它

   <?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 :(得分:-2)

你需要这样的事情还是请详细说明你的问题以便我能帮助你

<?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>
相关问题