如何将TouchImageView宽度设置为Device的宽度和高度的一半作为设备的宽度?

时间:2015-07-16 12:10:20

标签: android android-layout android-activity

我想在SELECT LTRIM( COALESCE(FirstName, '') + ' ' + COALESCE(MiddleName, '') + ' ' + COALESCE(LastName, '') ) AS FullName FROM EmpName 上显示两个TouchImageViews。我想将每个main activity的宽度设置为设备TouchImageView TouchImageView`的一半宽度到设备宽度的宽度。我试过但没有得到我的欲望结果。我想得到如下图像的结果。

enter image description here

我试过下面的代码......

s screen size. As well as I want to set height of each

MainActivity.Java

Display DeviceDisplay = getWindowManager().getDefaultDisplay(); Point size = new Point(); DeviceDisplay.getSize(size); int DeviceDisplayWidth = size.x; int DeviceDisplayHeight = size.y; RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(DeviceDisplayWidth / 2, DeviceDisplayHeight); img_to_be_zoomed.setLayoutParams(params); img_to_be_zoomed_mirror.setLayoutParams(params);

activity_main.xml

1 个答案:

答案 0 :(得分:0)

尝试此代码..将图像视图宽度和高度设置为运行时。

 private void setImageView() {
    // adding a view tree observer on image container

YourLayout.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            // remove predrawlistener
            YourLayout.getViewTreeObserver().removeOnPreDrawListener(this);
            setImageViewParams();
            return true;
        }
    });

    YourImageView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver
            .OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            // remove predrawlistener
            YourImageView.getViewTreeObserver().removeOnPreDrawListener(this);
            setImageViewParams();
            return true;
        }
    });
}

private void setImageViewParams() {
    if (YourImageView.getHeight() == 0 || rootView.getHeight() == 0) {
        return;
    }
    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) rootView.getLayoutParams();
    int imageViewPostion = (rootView.getWidth()/2);

    LinearLayout.LayoutParams imageViewLayoutParams = (Linear.LayoutParams) yourImageview.getLayoutParams();
    imageViewLayoutParams.setWidth = positonAfterImageContainer;
    iamgeViewLayoutParams.setHeight = positonAfterImageContainer;
    imageViewID.setLayoutParams(imageViewLayoutParams);
}