gpt4 book ai didi

android - 设置其他布局参数imageview

转载 作者:行者123 更新时间:2023-11-29 16:02:44 25 4
gpt4 key购买 nike

我想在 android 上有一个图像,只有当我点击一个按钮时才会出现。所以我在 onClick 方法中创建了它,并使用以下代码设置它的资源和位置:

public void createAddButton() {
//create the image
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.add_16);
//setting image position : width and height
imageView.setLayoutParams(
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT) );
}

但这只允许定义图像的宽度和高度。如果我想定义其他布局参数(例如android:layout_gravityandroid:marginLeft,我应该怎么做?

最佳答案

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.CENTER;
lp.bottomMargin = ...
lp.leftMargin = ...
lp.rightMargin = ...
lp.topMargin = ...

imageView.setLayoutParams(lp);

关于android - 设置其他布局参数imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22503678/

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