Android 带有ImageView和TextView的RelativeLayout-在ImageView下方对齐TextView,两者都以编程方式在RelativeLayout中居中
我与ImageView和TextView有一个相对论。我想在ImageView的正下方显示TextView(带有一个小的填充),同时显示ImageView和TextView,并在RelativeLayout中居中对齐 通过编程方式添加RelativeLayout 我已经阅读了一些关于对齐的问题,但是,我无法让它们为我工作。下面是我当前的代码 相对性代码Android 带有ImageView和TextView的RelativeLayout-在ImageView下方对齐TextView,两者都以编程方式在RelativeLayout中居中,android,android-layout,Android,Android Layout,我与ImageView和TextView有一个相对论。我想在ImageView的正下方显示TextView(带有一个小的填充),同时显示ImageView和TextView,并在RelativeLayout中居中对齐 通过编程方式添加RelativeLayout 我已经阅读了一些关于对齐的问题,但是,我无法让它们为我工作。下面是我当前的代码 相对性代码 RelativeLayout relativeLayout = new RelativeLayout(this); ImageView的代码
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\u,它们会相互重叠,这是可以理解的,因为RelativeLayout支持视图重叠
我认为为textview设置RelativeLayout.BELOW
会使其在图像下方对齐,但事实并非如此。我甚至尝试了textview的RelativeLayout.ALIGN_BOTTOM
,但即使这样也没用。试试这个
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);
或
以下流程也可能适用于您。
1) 在垂直线性布局中添加图像视图和文本视图。
2) 将线性布局添加到相对布局的中心。(使用“父”中的“中心”)我使用线性布局方法使其工作。谢谢你的回答。但是,有一个小小的变化。。使用、linearLayout.setGravity(Gravity.CENTER)代替layoutparams中的Gravity.CENTER,对我来说很有效。请相应地修改你的答案。谢谢回答已接受。@Hariharan感谢您分享您的代码,但我面临一个问题,当没有图像时,文本会移到中间。@Rstar只需删除linearLayout.setGravity(Gravity.center)代码>从父级LinearLayout
并重试。
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);