繁体   English   中英

如何在相对布局中调整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文本自动居中。

我能想到的使文本居中的唯一方法是使用填充,但我不喜欢它,它一定是更好的解决方案。

  1. 从资产调整图像大小不起作用

     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添加了TextViewImageView

创建一组资产图片和文本的最佳做法是什么,以便当我移动图片时,文字跟随(居中)

您始终可以将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。 不要更改textview的任何内容。

 frameLayout.addView(imageView);
 frameLayout.addView(text);

暂无
暂无

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

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