如何以编程方式使 TextView 与 ImageView 的中心对齐?
How to center TextView in alignment with center of ImageView programatically?
我正在动画函数中以编程方式制作 ImageView 和 TextView,ImageView 的大小会根据值而变化。
我只需要 TextView 的中心与图像视图对齐,我使用 RelativeLayout 参数 leftMargin 来确定它在 X 轴上的位置。
我尝试的任何方法似乎都不起作用,我正在尝试使用 imageView 和 TextView 的计算大小,但我并不真正理解那种数学。
我怎样才能简单地让这两个视图的中心对齐?在 swift 中,它会像 'imageView.centerXAxis' 一样简单,有没有我可以使用的等效项来代替 'leftMargin'?
这是函数,因为我正在尝试找出如何让视图居中对齐,所以它比我需要的要多得多。
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) {
}
});
}
我想让你的视图在 X 轴上对齐,它们在 Y 轴上应该有相同的值,方法是使用 [=12= 的 addRule()
方法] 这样:
textParams.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
textParams.addRule(RelativeLayout.ALIGN_BOTTOM, imageView.getId());
在这里,您要告诉 RelativeLayout
将您应用此参数的视图与 imageView
相对于其顶部和底部对齐。视图现在将是 imageView
的垂直大小,但是如果您将文本居中 setGravity(Gravity.CENTER)
(我看到您所做的),那么它将在 X 轴上与 [= 完美对齐14=].
我正在动画函数中以编程方式制作 ImageView 和 TextView,ImageView 的大小会根据值而变化。
我只需要 TextView 的中心与图像视图对齐,我使用 RelativeLayout 参数 leftMargin 来确定它在 X 轴上的位置。
我尝试的任何方法似乎都不起作用,我正在尝试使用 imageView 和 TextView 的计算大小,但我并不真正理解那种数学。
我怎样才能简单地让这两个视图的中心对齐?在 swift 中,它会像 'imageView.centerXAxis' 一样简单,有没有我可以使用的等效项来代替 'leftMargin'?
这是函数,因为我正在尝试找出如何让视图居中对齐,所以它比我需要的要多得多。
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) {
}
});
}
我想让你的视图在 X 轴上对齐,它们在 Y 轴上应该有相同的值,方法是使用 [=12= 的 addRule()
方法] 这样:
textParams.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
textParams.addRule(RelativeLayout.ALIGN_BOTTOM, imageView.getId());
在这里,您要告诉 RelativeLayout
将您应用此参数的视图与 imageView
相对于其顶部和底部对齐。视图现在将是 imageView
的垂直大小,但是如果您将文本居中 setGravity(Gravity.CENTER)
(我看到您所做的),那么它将在 X 轴上与 [= 完美对齐14=].