我的设计中有3个按钮。
根据条件,button3可以是可见的或不可见的。
现在3个按钮位于屏幕中央。如果其中一个可见,我想自动更改设计。
另外,我有一个活动的条形标记。我想在单击按钮时更改此标记的constraintStart。
提前致谢
这是我的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
}
});
}
}
答案 0 :(得分:1)
您需要更改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
实施