【问题标题】:Set the height of imageview according to screen from the layout根据布局中的屏幕设置imageview的高度
【发布时间】:2013-07-30 18:57:24
【问题描述】:

我正在制作布局并使用 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>

提前致谢

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    使用这个:

    int displayWidth = getWindowManager().getDefaultDisplay().getHeight();
    
    ImageView imageView = (ImageView)findViewById(R.id.image);
    
    imageView.getLayoutParams().height = displayWidth / 3;
    

    【讨论】:

    • 我想从 xml 布局做同样的事情。那我该怎么办?
    • 似乎可以工作,除了一种情况,如果图像高度小于屏幕高度的 1/3,则不会放大到屏幕高度的 1/3,编辑:使其工作我们需要在 ImageView xml 定义中设置android:scaleType="centerCrop"
    【解决方案2】:

    如果你像这样放置一个外部水平线性布局,你的图像视图将拥有 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>
    

    【讨论】:

    • 我用它设置了 imageview 1/3 但在 linearlayout 里面覆盖了屏幕的整个高度。我希望它也应该是 1/3。
    • 井重量被用作水平或垂直,所以你不能改变设置你的线性布局大小。您要求图像视图,这是实现它的方式。如果您也想要外部布局的精确结果,则必须以编程方式进行。
    【解决方案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="414dp"
            android:layout_weight="0.52"
            android:background="@drawable/ic_launcher" />
    
    </LinearLayout>
    

    【讨论】:

    • 从这段代码中它设置了宽度而不是高度。我想从 weightsum 和 layout_weight 属性中设置高度和宽度。
    • 上传图像你想如何显示你的布局我没有得到你的问题
    • 我的意思是我将上面的代码粘贴在根据屏幕宽度设置图像视图宽度的问题中,但我想根据屏幕设置高度和宽度 1/3。我成功地设置了宽度和高度,但不是从布局属性中设置两者
    • 检查我的答案,它同时处理宽度和高度。
    猜你喜欢
    • 2019-01-21
    • 2019-06-25
    • 2018-06-02
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多