[英]How to set Height Width of ImageView programmatically in android?
[英]how to set imageview's height and width using onTouch in android
我正在使用下面的代碼來設置imageview的寬度。 但它不起作用。
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" />`
和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;
}
}
}
當我觸摸圖像視圖時,它什么也沒做。
嘗試這個
RelativeLayout.LayoutParams param = (RelativeLayout.LayoutParams) v.getLayoutParams();
param.width=200;
v.setLayoutParams(param);
希望它對您有用。
嘗試這個:
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;
}
}
}
使用LayoutParams設置高度和寬度:
RelativeLayout layout = new RelativeLayout( this );
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
您可以使用View.getLayoutParams從代碼訪問任何LayoutParams
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.