4

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

请帮助我从布局 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>

提前致谢

4

3 回答 3

14

用这个:

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

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

imageView.getLayoutParams().height = displayWidth / 3;
于 2013-07-30T10:51:38.413 回答
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>
于 2013-07-30T10:57:24.133 回答
-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>
于 2013-07-30T10:49:03.997 回答