android - 根据布局中的屏幕设置imageview的高度

标签 android android-layout

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

请帮我从布局 xml 中将 imageview 高度设置为屏幕的 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>

提前致谢

最佳答案

使用这个:

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

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

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

关于android - 根据布局中的屏幕设置imageview的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17944910/

相关文章:

android - 添加 TextViews 并更新 ViewGroup 以显示 Textviews

选择 fragment 时Android底部导航栏项目背景颜色发生变化

android - 某个 png 图像从布局中消失了

java - Android Studio : How to change the Background to jpg. 图像使用 OnclickListener

java - 使用 AppBarLayout.Behavior.DragCallback 控制折叠工具栏布局的滚动

android - 标签没有显示出来

android - 具有自定义 View 的 RadioButton Android

android - 应用程序从已签名的 APK 而不是 Android Studio 崩溃

android - ChatSecure应用程序崩溃

android - 发生意外错误 : ant jar >/dev/null exited with 127 in phonegap