如何在相对布局中调整大小并对齐 TextView 到 ImageView - android sdk 10
How to resize and align to center TextView to ImageView in relative layout - android sdk 10
我正在玩 Android SDK 10
(我想支持来自 sdk 10
的设备)
我不知道如何在 Imageview
中自动居中文本。
我能想到的使文本居中的唯一方法是使用填充,但我不喜欢它,它必须是更好的解决方案。
无法调整资产中的图像大小
InputStream ims = context.getAssets().open("myimage.png");
Drawable d = Drawable.createFromStream(ims, null);
d.setBounds(0, 0, (int)(d.getIntrinsicWidth()*0.5),
(int)(d.getIntrinsicHeight()*0.5));
ScaleDrawable sd = new ScaleDrawable(d,0,px,py);
ims.close();
Image = new ImageView(context);
Image.setImageDrawable(sd);
Image.setAdjustViewBounds(true);
layout.addView(Image);
TextView t = new TextView(this.context);
t.setText(r.toString());
t.setTextSize(80);
t.setGravity(Gravity.CENTER);
layout.addView(t);
有没有办法将文本添加到图像资产 (ImageView
)?
现在我在 RelativeLayout
中添加了 TextView
和 ImageView
。
创建一组资产图像和文本的最佳做法是什么,以便在我移动图像时文本跟随(居中)
您始终可以将 CENTER_HORIZONTAL 规则添加到 RelativeLayout,使其所有子项保持水平居中。
试试这个。
将 Framelayout 保留为添加视图的父布局。
现在使用 LayoutParams 设置 ImageView 的重力、高度和宽度。
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER;
imageView.setLayoutParams(layoutParams);
现在在FrameLayout中先添加Imageview,再添加textview。不要为文本视图更改任何内容。
frameLayout.addView(imageView);
frameLayout.addView(text);
这个有效
结合@Dhananjay Sarsonia的回答
我正在玩 Android SDK 10
(我想支持来自 sdk 10
的设备)
我不知道如何在 Imageview
中自动居中文本。
我能想到的使文本居中的唯一方法是使用填充,但我不喜欢它,它必须是更好的解决方案。
无法调整资产中的图像大小
InputStream ims = context.getAssets().open("myimage.png"); Drawable d = Drawable.createFromStream(ims, null); d.setBounds(0, 0, (int)(d.getIntrinsicWidth()*0.5), (int)(d.getIntrinsicHeight()*0.5)); ScaleDrawable sd = new ScaleDrawable(d,0,px,py); ims.close(); Image = new ImageView(context); Image.setImageDrawable(sd); Image.setAdjustViewBounds(true); layout.addView(Image); TextView t = new TextView(this.context); t.setText(r.toString()); t.setTextSize(80); t.setGravity(Gravity.CENTER); layout.addView(t);
有没有办法将文本添加到图像资产 (ImageView
)?
现在我在 RelativeLayout
中添加了 TextView
和 ImageView
。
创建一组资产图像和文本的最佳做法是什么,以便在我移动图像时文本跟随(居中)
您始终可以将 CENTER_HORIZONTAL 规则添加到 RelativeLayout,使其所有子项保持水平居中。
试试这个。 将 Framelayout 保留为添加视图的父布局。 现在使用 LayoutParams 设置 ImageView 的重力、高度和宽度。
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.gravity = Gravity.CENTER;
imageView.setLayoutParams(layoutParams);
现在在FrameLayout中先添加Imageview,再添加textview。不要为文本视图更改任何内容。
frameLayout.addView(imageView);
frameLayout.addView(text);
这个有效
结合@Dhananjay Sarsonia的回答