我可以使用ConstraintSet以编程方式修改ConstraintLayout中视图的约束,但在应用修改之前,我想测试它尚未完成。为此,我需要以编程方式访问ConstraintLayout中视图的当前约束。
例如,在这里,我想删除TextView“title”顶部和TextView视图“subtitle”底部之间的约束。然后将TextView'title'的顶部限制在'videoView'的底部。但首先我需要检查TextView'标题'是否已经被限制在'videoView'的底部(从android's official website修改的代码)。注意,我不能在这个网站上插入最后一行HTML,它是尖括号中的/android.support.constraint.ConstraintLayout。
vec4 EncodeExp( in float value )
{
int exponent = int( log2( abs( value ) ) + 1.0 );
value /= exp2( float( exponent ) );
value = (value + 1.0) * (256.0*256.0*256.0 - 1.0) / (2.0*256.0*256.0*256.0);
vec4 encode = fract( value * vec4(1.0, 256.0, 256.0*256.0, 256.0*256.0*256.0) );
return vec4( encode.xyz - encode.yzw / 256.0 + 1.0/512.0, (float(exponent) + 127.5) / 256.0 );
}
float DecodeExp( in vec4 pack )
{
int exponent = int( pack.w * 256.0 - 127.0 );
float value = dot( pack.xyz, 1.0 / vec3(1.0, 256.0, 256.0*256.0) );
value = value * (2.0*256.0*256.0*256.0) / (256.0*256.0*256.0 - 1.0) - 1.0;
return value * exp2( float(exponent) );
}
}
public class MainActivity extends AppCompatActivity {
ConstraintSet mConstraintSet = new ConstraintSet(); // create a Constraint Set
ConstraintLayout mConstraintLayout; // cache the ConstraintLayout
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConstraintLayout = (ConstraintLayout) findViewById(R.id.root);
mConstraintSet.clone(mConstraintLayout); // get constraints from ConstraintSet
}
public void userDoesSomething(View v) {
//test if title has already been constrained to bottom of videoView
if (mConstraintLayout 'TITLE' IS NOT CONSTRAINED TO THE BOTTOM OF 'VIDEOVIEW'){
mConstraintSet.connect(R.id.Title, ConstraintSet.TOP, R.id.videoView, ConstraintSet.BOTTOM); // set new constraints to ConstraintSet
mConstraintSet.applyTo(mConstraintLayout); // set new constraints to ConstraintLayout
}
}
答案 0 :(得分:1)
对我的承认,没有办法,你可以试着比较两个ConstraintLayout.LayoutParams,例如:
public class MainActivity extends AppCompatActivity {
ConstraintSet mConstraintSet = new ConstraintSet(); // create a Constraint Set
ConstraintLayout mConstraintLayout; // cache the ConstraintLayout
int mConstraintValue;
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConstraintLayout = (ConstraintLayout) findViewById(R.id.root);
mTextView = (TextView) findViewById(R.id.title);
mConstraintSet.clone(mConstraintLayout); // get constraints from ConstraintSet
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mTextView.getLayoutParams();
mConstraintValue = params.topToBottom;
}
public void userDoesSomething(View v) {
//test if title has already been constrained to bottom of videoView
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) mTextView.getLayoutParams();
if (mConstraintValue.equals(params.topToBottom)){
mConstraintSet.connect(R.id.Title, ConstraintSet.TOP, R.id.videoView, ConstraintSet.BOTTOM); // set new constraints to ConstraintSet
mConstraintSet.applyTo(mConstraintLayout); // set new constraints to ConstraintLayout
}
}
}
希望这有帮助。