如何通过Java code中置布局,以垂直于Android的? [英] how to center layout to vertical in android through java code?

查看:104
本文介绍了如何通过Java code中置布局,以垂直于Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,

我想设置的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.

这篇关于如何通过Java code中置布局,以垂直于Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆