![](/img/trans.png)
[英]Align ImageView to center of a TextView in RelativeLayout
[英]RelativeLayout with ImageView and TextView - Align TextView below ImageView, both center in RelativeLayout Programatically
我有一个带有ImageView和TextView的RelativeLayout。 我想将TextView放在ImageView(带有少量填充)的正下方,并将ImageView和TextView都对齐到RelativeLayout的中心。
RelativeLayout以编程方式添加
我已经阅读了一些有关对齐的问题,但是,我无法让它们为我工作。 下面是我当前的代码...
RelativeLayout的代码
RelativeLayout relativeLayout = new RelativeLayout(this);
ImageView的代码
ImageView image = new ImageView(this);
RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpImage.addRule(RelativeLayout.CENTER_IN_PARENT);
//Setting the parameters on the Image
image.setLayoutParams(lpImage);
//adding imageview to relative layout
relativeLayout.addView(image);
TextView的代码
TextView textview = new TextView(this);
RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpTextView.addRule(RelativeLayout.BELOW, image.getId());
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
//Adding TextView to relative layout
relativeLayout.addView(textview);
如果我为图像和文本都设置了RelativeLayout.CENTER_IN_PARENT
,它们彼此重叠,这是可以理解的,因为RelativeLayout支持视图的重叠。
我认为为textview设置RelativeLayout.BELOW
将使其在图像下方对齐,但事实并非如此。 我什至尝试了RelativeLayout.ALIGN_BOTTOM
进行textview,但是即使这样也没有用。
尝试这个..
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams lprela = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
relativeLayout.setLayoutParams(lprela);
ImageView image = new ImageView(this);
RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpImage.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//Setting the parameters on the Image
image.setLayoutParams(lpImage);
//adding imageview to relative layout
relativeLayout.addView(image);
TextView textview = new TextView(this);
RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lpTextView.addRule(RelativeLayout.BELOW, image.getId());
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
//Adding TextView to relative layout
relativeLayout.addView(textview);
要么
LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(lp_ineer_ver);
ImageView image = new ImageView(this);
LinearLayout.LayoutParams lpImage = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//Setting the parameters on the Image
image.setLayoutParams(lpImage);
//adding imageview to relative layout
linearLayout.addView(image);
TextView textview = new TextView(this);
LinearLayout.LayoutParams lpTextView = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
//Setting the parameters on the TextView
textview.setLayoutParams(lpTextView);
//Adding TextView to relative layout
linearLayout.addView(textview);
以下过程也可能为您服务。 1)在垂直LinearLayout中添加图像视图和文本视图。 2)将线性布局添加到相对布局的中心。(使用CENTER_IN_PARENT)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.