gpt4 book ai didi

android - RelativeLayout 与 ImageView 和 TextView - 在 ImageView 下方对齐 TextView,两者都以编程方式居中于 RelativeLayout

转载 作者:太空狗 更新时间:2023-10-29 16:38:33 31 4
gpt4 key购买 nike

我有一个带有 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 支持 View 重叠。

我认为为 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);

或者

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);

关于android - RelativeLayout 与 ImageView 和 TextView - 在 ImageView 下方对齐 TextView,两者都以编程方式居中于 RelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22167134/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com