简体   繁体   English

如何以编程方式使TextView与ImageView的中心对齐?

[英]How to center TextView in alignment with center of ImageView programatically?

I am making an ImageView and TextView programatically in an animation function, the ImageView size changes depending on a value. 我在动画函数中以编程方式制作了ImageView和TextView,ImageView的大小根据值而变化。

I simply need the TextView's center to be aligned with the image views, I am using RelativeLayout parameter leftMargin to determine its location on the X axis. 我只需要将TextView的中心与图像视图对齐,即可使用RelativeLayout参数leftMargin来确定其在X轴上的位置。

Nothing I try seems to work, I am trying to use the calculated size of the imageView and the TextView but I don't really understand that kind of maths. 我尝试执行的所有操作似乎都没有效果,但我尝试使用计算得出的imageView和TextView的大小,但我并不真正理解这种数学方法。

How can I simply get the centers of these two Views to align? 如何简单地使这两个视图的中心对齐? In swift it would be as simple as 'imageView.centerXAxis' is there any equivalent of that that I could use instead of the 'leftMargin'? 迅速地,它就像“ imageView.centerXAxis”一样简单,是否可以代替“ leftMargin”来使用?

Here is the function, there is a lot more than needed as I am trying to find out how to get the views to align in the center. 这是函数,有很多需要解决的问题,因为我试图找出如何使视图在中心对齐。

   void heartFlurry(String username, Integer value) {

    Drawable heart = getResources().getDrawable( R.drawable.heart );
    View v = new ImageView(getBaseContext());
    ImageView imageView;
    imageView = new ImageView(v.getContext());
    imageView.setImageDrawable(heart);

    final TextView usernameLabel = new TextView(this);
    usernameLabel.setText(username);
    usernameLabel.setTextColor(Color.WHITE);

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    Double widthMax = size.x * 0.8;
    Double widthMin = size.x * 0.2;
    int max = (int) Math.round(widthMax);
    int min = (int) Math.round(widthMin);
    Log.e("Width", "" + width);
    Log.e("height", "" + height);

    int heartWidthOriginal = heart.getIntrinsicWidth();
    int heartHeightOriginal = heart.getIntrinsicHeight();
    int newValue = value * 25;
    int heartWidth = (heart.getIntrinsicWidth() / 2 + newValue);
    int heartHeight = (heart.getIntrinsicHeight() / 2 + newValue);

    Log.e("HeartWidth", "" + heartWidth);
    Log.e("HeartHeight", "" + heartHeight);
    Log.e("HeartWidthOriginal", "" + heartWidthOriginal);
    Log.e("HeartHeightOriginal", "" + heartHeightOriginal);

    final int randomX = new Random().nextInt((max - min) + 1) + min;
    Log.e("randomX", "" + randomX);

    relativeLayout.addView(imageView);
    imageView.setId(View.generateViewId());
    relativeLayout.addView(usernameLabel);
    usernameLabel.setId(View.generateViewId());

    usernameLabel.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    usernameLabel.setGravity(Gravity.CENTER);
    usernameLabel.setTextSize(22);

    RelativeLayout.LayoutParams heartParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );
    heartParams.leftMargin = randomX;
    heartParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

    RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );

    imageView.setLayoutParams(heartParams);
    imageView.getLayoutParams().height = heartHeight;
    imageView.getLayoutParams().width = heartWidth;

    imageView.requestLayout();  // Think the important stuff starts here:

    usernameLabel.measure(0, 0);       //must call measure!
    usernameLabel.getMeasuredWidth();
    Integer textWidth = usernameLabel.getMeasuredWidth();
    Integer halfHeartWidth = heartWidth/2;
    System.out.println("TEXTWIDTH" + textWidth);
    textParams.leftMargin = randomX + (textWidth*halfHeartWidth / randomX);
    textParams.addRule(RelativeLayout.BELOW, imageView.getId());
    textParams.addRule(RelativeLayout.CENTER_VERTICAL, imageView.getId());
    textParams.topMargin = 25;

    usernameLabel.setLayoutParams(textParams);

    ObjectAnimator animationHeartY = ObjectAnimator.ofFloat(imageView, "translationY", -size.y);
    animationHeartY.setDuration(2000);

    ObjectAnimator animationTextViewY = ObjectAnimator.ofFloat(usernameLabel, "translationY", -size.y);
    animationTextViewY.setDuration(2000);

    animationHeartY.start();
    animationTextViewY.start();
    animationTextViewY.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
           usernameLabel.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    });

}

I you want your views to be aligned in the X axis, they should have the same values in the Y axis, and the way to do that is using the method addRule() of the RelativeLayout.LayoutParams this way: 我希望您的视图在X轴上对齐,它们在Y轴上应该具有相同的值,而这样做的方法是使用RelativeLayout.LayoutParams的方法addRule()

textParams.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
textParams.addRule(RelativeLayout.ALIGN_BOTTOM, imageView.getId());

Here you are telling to the RelativeLayout to align the view you are aplying this params with the imageView , relative to its top and to its bottom. 在这里,你告诉给RelativeLayout对准你aplying这个PARAMS与视图imageView ,相对于它的顶部和底部。 The view will now be the size of the imageView vertically, but if you center the text with setGravity(Gravity.CENTER) (what I saw you did), then it will be perfectly aligned in the X axis with the imageView . 该视图现在将是大小imageView垂直,但如果你中心与文本setGravity(Gravity.CENTER)我看到你做了),那么它将被完全与x轴一致imageView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM