如何通过Java code中置布局,以垂直于Android的?布局、Java、code、Android

2023-09-07 04:56:26 作者:胜者为↗王

朋友,

我想设置的android:layout_centerVertical =真正的布局性 通过Java $ C $图像℃。

i want to set android:layout_centerVertical="true" property of layout through java code of an image.

任何一个可以指导我如何实现这一目标。这里是我的code。

can any one guide me how to achieve this. here is my code.

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

params.height = (int)totalHeight;

img.setLayoutParams(params);

我已经使用setScaleType(ScaleType.FIT_CENTER),但没有用的尝试。

i have tried using setScaleType(ScaleType.FIT_CENTER) but no use.

任何帮助将appriciated。

any help would be appriciated.

推荐答案

纠正我,如果我错了,但它听起来就像你正试图设置图像的调整(或位置方向)的布局里面?要做到这一点,你需要设置包含您对齐视图布局的重心财产。

Correct me if I'm wrong but it sounds like you are trying to set the alignment (or positional orientation) of an image inside of a layout? To do that you need to set the gravity property of the layout containing the View you align.

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.FILL_PARENT, 
            RelativeLayout.LayoutParams.WRAP_CONTENT);

    ImageView imageView = new ImageView(this);
    imageView.setLayoutParams(params);
    imageView.setImageBitmap(bitmap);

    layout.setGravity(Gravity.CENTER_VERTICAL | Gravity.TOP);
    layout.addView(imageView);

在这个例子中,我以编程方式添加图片到我的布局资源一个RelativeLayout的,添加ImageView的,并调整它,所以它会被放置在顶部,垂直的中心位置。

In this example I'm programmatically adding an image to a RelativeLayout in my layout resource, adding an ImageView, and aligning it so it will be placed at the top, vertical center position.