I am using below code to set an imageview's width. but it is not working.
xml:
`
<ImageView
android:id="@+id/card2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="3dp"
android:layout_marginTop="120dp"
android:layout_toRightOf="@+id/card1"
android:contentDescription="@string/slot"
android:src="@drawable/spade_3" />`
and java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play__computer);
card1 = (ImageView)findViewById(R.id.card1);
card2 = (ImageView)findViewById(R.id.card2);
card1.setOnTouchListener(new ChoiceTouchListener());
card2.setOnTouchListener(new ChoiceTouchListener());
}
private final class ChoiceTouchListener implements OnTouchListener{
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
v.getLayoutParams().width = 100;
return true;
} else{
return false;
}
}
}
when i touch the image view, it does nothing.
Try this
RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) v.getLayoutParams();
param.width=200;
v.setLayoutParams(param);
hope it will work for you.
Try this:
private final class ChoiceTouchListener implements OnTouchListener{
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
ImageView imgView = (ImageView) v;
int width = 100;
int height = 100;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(width, height);
imgView.setLayoutParams(layoutParams);
return true;
} else{
return false;
}
}
}
Use LayoutParams to set the height and width:
RelativeLayout layout = new RelativeLayout( this );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
You can access any LayoutParams from code using View.getLayoutParams
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.