Android在相对布局前面设置ImageView

时间:2014-02-03 18:48:39

标签: android android-layout imageview relativelayout

我的Android应用中有RelativeLayout。现在我想在该布局前面显示ImageView。问题是ImageView不在前面,它有点透明,我可以看到EditTextButton之类的内容。我无法更改布局(setContentView),因为布局是动态创建的,在setContentView之后,控件不在。

2 个答案:

答案 0 :(得分:3)

您可以通过编程方式添加视图,以及它在顶部的方式!

  1. 为您的顶级布局创建ID
  2. 现在有些代码(在我的例子中是相对布局):

    RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative_layout_id);
    ImageView imageView = new ImageView(context)  
    Drawable rightArrowBlackDrawable = getResources().getDrawable(R.drawable.image); 
    imageView.setLayoutParams(getLayoutParams());
    relativeLayout.addView(imageView);
    imageView.bringToFront();    
    
    //here just example layout params, use yours params ;-) 
    private RelativeLayout.LayoutParams getLayoutParams() {
    
        RelativeLayout.LayoutParams layoutParams = 
        new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,             
        RelativeLayout.LayoutParams.WRAP_CONTENT);
    
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        return layoutParams;
    }
    

答案 1 :(得分:0)

插入后,您可以将它带到前面。

imageView.bringToFront();

如果图像是透明的,您可以设置白色背景以防止其下方显示。

imageView.setBackgroundColor(0xFFFFFF);