gpt4 book ai didi

android - 如何通过代码将 ImageView 设置在布局的中心?

转载 作者:行者123 更新时间:2023-11-29 21:40:19 25 4
gpt4 key购买 nike

接下来试试

LinearLayout.LayoutParams b = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
b.gravity =Gravity.CENTER;

ivOne.setLayoutParams(b);

但这不起作用,布局左侧的 ImageView

最佳答案

尝试将父布局更改为 RelativeLayout ,并使用属性 centerInParent :

RelativeLayout rootLayout = new RelativeLayout(this);
rootLayout.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
ImageView ivOne = new ImageView(this);
ivOne.setImageResource(R.drawable.ic_launcher);
ivOne.setScaleType(ImageView.ScaleType.CENTER);
ivOne.setLayoutParams(params);
//TODO : add other views
rootLayout.addView(ivOne);

关于android - 如何通过代码将 ImageView 设置在布局的中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17339199/

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