gpt4 book ai didi

java - Android:在 Java 代码中更改 layout_constraintStart point 和 layout_margin

转载 作者:行者123 更新时间:2023-11-30 00:21:07 26 4
gpt4 key购买 nike

我的设计中有 3 个按钮。

按钮3可以根据条件可见或不可见。

现在 3 个按钮位于屏幕中央。如果其中之一将可见,我想自动更改设计。

此外,我还有一个活跃的条形标记。我想在单击按钮时更改此标记的 constraintStart。

提前致谢

Screenshot

这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"

tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="25dp">


<Button
android:id="@+id/btn1"
style="?android:attr/borderlessButtonStyle"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginStart="8dp"
android:text="Button1"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/btn2" />

<Button
android:id="@+id/btn2"
style="?android:attr/borderlessButtonStyle"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:text="Button2"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@+id/btn3"
app:layout_constraintStart_toEndOf="@+id/btn1" />

<Button
android:id="@+id/btn3"
style="?android:attr/borderlessButtonStyle"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginRight="24dp"
android:layout_marginTop="16dp"
android:text="button3"
android:textSize="14sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imgActiveBar"
android:layout_width="103dp"
android:layout_height="4dp"
android:layout_marginStart="8dp"
android:background="@android:color/holo_blue_dark"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="@+id/btn1"
app:layout_constraintTop_toBottomOf="@+id/btn1" />


</android.support.constraint.ConstraintLayout>

这是我的 Java 代码:

package au.com.test.listviewtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class TestActivity extends AppCompatActivity {
private boolean bCondition = false;
private Button btn1 = null;
private Button btn2 = null;
private Button btn3 = null;
private ImageView imgActiveBar = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);

btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
imgActiveBar = (ImageView) findViewById(R.id.imgActiveBar);


if (bCondition==true){
btn3.setVisibility(View.INVISIBLE);
//If bt3 is invisible than
// make btn1 an btn2 centered


} else {
btn3.setVisibility(View.VISIBLE);
//If bt3 is visible than
// make btn1 an btn2 and bt3 centered

}

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// make ActiveBar's layout_constraintStart_toStartOf is bt1
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// make ActiveBar's layout_constraintStart_toStartOf is bt2
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// make ActiveBar's layout_constraintStart_toStartOf is bt3
}
});


}
}

最佳答案

您需要更改 imgActiveBar 布局参数的 startToStart 字段:

 ConstraintLayout.LayoutParams prams = (ConstraintLayout.LayoutParams) imgActiveBar.getLayoutParams();
prams.startToStart = btn1.getId(); //for example
//you'll probably need to change the end position as well
prams.endToEnd = btn1.getId();
imgActiveBar.setLayoutParams(prams);
imgActiveBar.requestLayout();

此行为由 TabLayout 实现

关于java - Android:在 Java 代码中更改 layout_constraintStart point 和 layout_margin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46261553/

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