gpt4 book ai didi

android studio margin 变化

转载 作者:行者123 更新时间:2023-11-29 02:39:36 38 4
gpt4 key购买 nike

我尝试进行研究,但我无法做出正面或反面的结论,因为我对此太陌生了。

我正在使用约束布局。我有一个按钮,button1。button1 的底部边距为 10。

有没有一种简单的方法可以让我点击我的按钮时,它会将底部边距更改为 20?

这是我的简单代码:

button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (butt1){
button1.setBackgroundResource(buttonpress2);
butt1 = false;
}else{
button1.setBackgroundResource(buttonpress);
butt1 = true;
}
}
});

还有:

    <Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/buttonpress"
android:text="@string/button1Text"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginBottom="10dp"
android:layout_marginLeft="38dp" />

最佳答案

与其创建一个新的布局参数对象,不如用 View 已有的对象实例化它...这样它将保留您在 xml 中设置的所有属性,并在您更改此处后更新。

这样做....

int marginInPixels = dpToPx(10);

public int dpToPx(int dp) {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}
ConstraintLayout.LayoutParams params =
(ConstraintLayout.LayoutParams)button1.getLayoutParams();

params.topMargin = marginInPixels;
button1.setLayoutParams(params);

关于android studio margin 变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45108435/

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