Android:以编程方式设置 ImageView layout_height

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11911011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:08:55  来源:igfitidea点击:

Android: Set ImageView layout_height programmatically

androidandroid-imageview

提问by capcom

I have an ImageView in my main.xml file:

我的 main.xml 文件中有一个 ImageView:

<ImageView
            android:id="@+id/pageImage"
            android:layout_width="270dp"
            android:layout_height="45dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="25dp"
            android:layout_marginTop="15dp"
            android:scaleType="fitXY"
            android:src="@drawable/pageimage" />

What I only need to do is to set the the layout_heightprogrammatically to another value.

我只需要做的是以layout_height编程方式将 设置为另一个值。

Additionally, I need to set the value in dp. So it is currently 45dp, I'd like to change it to another value in my main activity.

此外,我需要在dp 中设置值。所以它目前是 45dp,我想在我的主要活动中将它更改为另一个值。

I tried to figure out how to do this using LayoutParams, but I couldn't succeed.

我试图弄清楚如何使用 LayoutParams 做到这一点,但我无法成功。

Thanks.

谢谢。

回答by Tobias Reich

I think you want something like this:

我想你想要这样的东西:

ImageView imgView = (ImageView) findViewById(R.id.pageImage);
imgView.getLayoutParams().height = 200;

So first we get the ImageView from the XML. Then getLayoutParams() gets you exactly the params you may edit then. You can set them directly there. The same for the width etc.

所以首先我们从 XML 中获取 ImageView。然后 getLayoutParams() 准确地为您提供您可以编辑的参数。您可以直接在那里设置它们。宽度等相同。

About the unit (sp,dp...) I found something here. Hope this helps:

关于单位 (sp,dp...) 我在这里找到了一些东西。希望这可以帮助:

Use DIP, SP metrics programmatically

以编程方式使用 DIP、SP 指标

There they mention, that all units are pixels. In the example he sets the minimum widthof 20sp like this:

他们在那里提到,所有单位都是像素。在示例中,他将最小宽度设置为 20sp,如下所示:

TextView tv0 = new TextView(this);
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics());
tv0.setMinimumWidth(px);

P.S. Here is the complete code I use. Hopefully this is what you want:

PS这是我使用的完整代码。希望这是你想要的:

package com.example.testproject2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView imgView = (ImageView) findViewById(R.id.pageImage);
        imgView.getLayoutParams().height = 500;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

回答by 0gravity

You can do something like this:

你可以这样做:

LinearLayout.LayoutParams layoutParams  = new LinearLayout.LayoutParams(width_physical_pixels, height_physical_pixels);
imageView.setLayoutParams(layoutParams);

But in the parameters it takes a pixel, so you would have to use the formula to convert pixels to dp, for example you can use this to convert:

但是在参数中它需要一个像素,因此您必须使用公式将像素转换为 dp,例如您可以使用它来转换:

float scale = getBaseContext().getResources().getDisplayMetrics().density;    
px = dp_that_you_want * (scale / 160);

Putting everything together:

把所有东西放在一起:

//Get screen density to use in the formula.
float scale = getBaseContext().getResources().getDisplayMetrics().density;    
px = dp_that_you_want * (scale / 160);

LinearLayout.LayoutParams layoutParams  = new LinearLayout.LayoutParams(px, px);
imageView.setLayoutParams(layoutParams);

I don't know if this is what you are looking for, or if it will work, so let me know if it does.

我不知道这是否是您正在寻找的内容,或者它是否有效,所以请告诉我是否可行。