我创建了ImageView
和Button
,并希望在点击或按下imageView
后缩小button
尺寸并设置ImageView
回到默认状态。
这是否可行,如果是这样,我将如何实现这一目标并设置
LayoutParams.width = 168dp
?
ImageView代码:
<ImageView
android:layout_width="168dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:id="@+id/imgCircle"
android:background="@drawable/ic_img_circle"
android:clickable="false"/>
按键代码:
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/btnSmall"
android:text="Small"
android:layout_marginLeft="5dp"
android:background="@mipmap/ic_bg_button"
android:textStyle="bold"
android:textColor="@color/colorBlack"/>
尝试过的代码:
@Override
public void onClick(View v) {
if ( v == NEXT ) {
android.view.ViewGroup.LayoutParams layoutParams = TEST.getLayoutParams();
layoutParams.width = 168; //Need this to be 168dp
layoutParams.height = 30; //Need this to be 30dp
TEST.setLayoutParams(layoutParams);
TEST.setBackgroundResource(R.mipmap.ic_bg_button);
Random r = new Random();
random = r.nextInt(7 - 1) + 1;
if(random == 1) {
textQueston.setText("Spin the Circle Clockwise");
}
else if(random == 2) {
textQueston.setText("Make the Circle Blue");
}
else if(random == 3) {
textQueston.setText("Make the Circle Red");
}
else if(random == 4) {
textQueston.setText("Spin the Circle Anti-Clockwise");
}
else if(random == 5) {
textQueston.setText("Make Circle Smaller");
}
else if(random == 6) {
textQueston.setText("Make Circle Larger");
}
}
if ( v == BLUE ) {
if (random == 2 ) {
TEST.setBackgroundColor(Color.BLUE);
}
}
else if ( v == RED ) {
if (random == 3) {
TEST.setBackgroundColor(Color.RED);
}
}
else if ( v == CLOCKWISE ) {
if (random == 1) {
Animation aniRotateClk = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.button_rotate_clockwise);
TEST.startAnimation(aniRotateClk);
/* start Animation */
//TEST.startAnimation(rotation);
}
}
else if (v == ANTICLOCKWISE) {
if (random == 4) {
Animation animRotateAclk = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.button_rotate_anticlockwise);
TEST.startAnimation(animRotateAclk);
}
}
else if( v == SMALL) {
if (random == 5) {
android.view.ViewGroup.LayoutParams layoutParams = TEST.getLayoutParams();
layoutParams.width = 150; //Need this to be 150dp
layoutParams.height = 20; //Need this to be 20dp
TEST.setLayoutParams(layoutParams);
}
}
else if ( v == LARGE) {
if ( random == 6) {
android.view.ViewGroup.LayoutParams layoutParams = TEST.getLayoutParams();
layoutParams.width = 200; //Need this to be 200dp
layoutParams.height = 50; //Need this to be 50dp
TEST.setLayoutParams(layoutParams);
}
}
}
非常感谢任何帮助。
由于
答案 0 :(得分:0)
您应该为按钮设置onOnClickListener,然后每次单击为imageView设置LayoutParams。
搜索关键字“setLayoutParams for imageView”
答案 1 :(得分:0)
使用以下代码更改height
width
ImageView
Button
点按getLayoutParams().height
点击/点击:
你可以增加/减少身高&amp;宽度getLayoutParams().width
&amp; btnSmall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imgCircle.getLayoutParams().height = 500;
imgCircle.getLayoutParams().width = 500;
imgCircle.setScaleType(ImageView.ScaleType.FIT_XY);
}
});
@Override
public void onClick(View v) {
if ( v == NEXT ) {
android.view.ViewGroup.LayoutParams layoutParams = TEST.getLayoutParams();
TEST.setLayoutParams(layoutParams);
TEST.setBackgroundResource(R.mipmap.ic_bg_button);
layoutParams.width = 168; //Need this to be 168dp
layoutParams.height = 30; //Need this to be 30dp
Random r = new Random();
random = r.nextInt(7 - 1) + 1;
if(random == 1) {
textQueston.setText("Spin the Circle Clockwise");
}
else if(random == 2) {
textQueston.setText("Make the Circle Blue");
}
else if(random == 3) {
textQueston.setText("Make the Circle Red");
}
else if(random == 4) {
textQueston.setText("Spin the Circle Anti-Clockwise");
}
else if(random == 5) {
textQueston.setText("Make Circle Smaller");
}
else if(random == 6) {
textQueston.setText("Make Circle Larger");
}
}
根据我的代码更改您的代码:
int, int, double
答案 2 :(得分:0)
ImageView imageView = findViewById(R.id.dl_image);
imageView.getLayoutParams().width = 120;