[英]Why I can't see the image? (ImageView)
我想創建動態ImageView,但它沒有出現,有什么幫助嗎?
@Override public boolean onTouchEvent(MotionEvent event) {
RelativeLayout myLayout = new RelativeLayout(this);
ImageView stone = new ImageView(this);
stone.setImageResource(R.drawable.pedra);
stone.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
myLayout.addView(stone, lp);
return true;
}
試試這個,它會幫助你
<RelativeLayout
android:id="@+id/relativelayout"....>
</RelativeLayout>
public class MainActivity extends Activity {
private RelativeLayout rLayout;
@Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.main);
rlayout = (RelativeLayout) findViewById(R.id.relativeLayout);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
ImageView stone = new ImageView(this);
stone.setImageResource(R.drawable.pedra);
stone.setVisibility(View.VISIBLE);
rLayout.addView(stone);
return true;
}
}
你的代碼是正確的,但沒有太大的影響,因為它是藝術的藝術。 您創建RelativeLayout
,然后創建ImageView
,然后將ImageView
添加到RelativeLayout
。 這樣可行,但要使其顯示您的布局必須添加到顯示的層次結構中。 所以我將RelativeLayout
添加到你的xml布局文件中,用android:id
id
為它分配id
,然后在你的觸摸上做findViewById()
並在那里添加創建的ImageView
。
你忘了給你的myLayout
充氣了
對於某些設備,也將所有圖像放在drawable-xhdpi目錄中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.