[英]How display some ImageView in a RelativeLayout?
我有一個相對布局
RelativeLayout myLayout = new RelativeLayout(this);
然后,我在此布局中添加 2 ImageView:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(20, 20);
ImageView img1 = new ImageView(this);
// Here I give the position of img1
ImageView img2 = new ImageView(this);
// And here the position and img2
myLayout.addView(img1, params);
myLayout.addView(img2, params);
setContentView(myLayout);
在這里我有一個問題:我想顯示並單擊 2 ImageViews,但我的屏幕上只有 img2 可見。
z-index 或其他問題是否存在問題?
由於您使用的是 RelativeLayout 並且params
的參數相同,因此一個將與另一個重疊。 所以為兩者定義參數。 為每個參數Add rule
以定位它。 然后給addView()
。
例如:
RelativeLayout myLayout = new RelativeLayout(this);
ImageView img1 = new ImageView(this);
RelativeLayout.LayoutParams firstImageParams = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
leftArrowParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
ImageView img2 = new ImageView(this);
RelativeLayout.LayoutParams secImageParams = new RelativeLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rightArrowParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
myLayout.addView(img1, firstImageParams);
myLayout.addView(img2, secImageParams);
setContentView(myLayout);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.