图像重叠在线性布局上

时间:2012-05-15 08:27:53

标签: android android-layout

我面临的问题是:

  1. 我创建了一个线性布局的动态按钮,其垂直方向位于相对布局的中心。
  2. 然后,我想在线性布局的顶部添加一个图像,该图像显示在第一个按钮的右下角。
  3. 但是,经过2天的努力,我仍然无法做到正确。 “图像”显示在第一个按钮的中央。

    我想要应用程序的图像。

    enter image description here

    这是特定的代码:

    ///outermost layout
    RelativeLayout L1 = new RelativeLayout(this);
    L1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    
    //For image overlapping on linear layout
    FrameLayout FrameLayout = new FrameLayout(this);
    RelativeLayout.LayoutParams RelativeParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    
    //Place all dynamic button inside here -vertical
    LinearLayout L2 = new LinearLayout(this);
    L2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    L2.setOrientation(LinearLayout.VERTICAL);
    
    ImageView handpointer;
    handpointer = new ImageView(this);
    
    //grab the imageview and load the animations
    handpointer.setImageDrawable(getResources().getDrawable(R.drawable.hand)); 
    

    这是在布局L2中添加所有动态按钮的代码。

        L2.addView(b);
        L2.addView(b);
    }
    
    RelativeLayout.LayoutParams Param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
    Param.setMargins(0, 70, 0, 0);
    L1.addView(FrameLayout,Param);
    
    Param.addRule(RelativeLayout.CENTER_HORIZONTAL);
    FrameLayout.addView(L2,Param);
    RelativeParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    FrameLayout.addView(handpointer);
    
    Drawable drawable = getResources().getDrawable(R.drawable.snail_menu); 
    L1.setBackgroundDrawable(drawable);
    
    this.setContentView(L1);
    

    非常感谢你的帮助!

2 个答案:

答案 0 :(得分:0)

将您的按钮放在XML中,无论您想在运行时显示它。只需使用setVisibility方法即可显示或隐藏它。这不是可行的解决方案吗?

答案 1 :(得分:0)

尝试使用此代码进行动态视图创建,它可能会对您有所帮助

///最外层布局

RelativeLayout L1 = new RelativeLayout(this);
L1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

//用于线性布局上的图像重叠

FrameLayout FrameLayout = new FrameLayout(this);
RelativeLayout.LayoutParams RelativeParam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

//将所有动态按钮放在此处 - 垂直

TableLayout TL = new TableLayout(this);
TL.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

ImageView handpointer;
handpointer = new ImageView(this);

//抓取imageview并加载动画

handpointer.setImageDrawable(getResources().getDrawable(R.drawable.hand));

TableRow tr1 = new TableRow(this);
tr1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TL.addView(tr1);
tr1.aadView(b1);


TableRow tr2 = new TableRow(this);
tr2.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TL.addView(tr2);
tr2.addView(handpointer);

TableRow tr3 = new TableRow(this);
tr3.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TL.addView(tr3);
tr3.addView(b2);

RelativeLayout.LayoutParams Param = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

Param.setMargins(0, 70, 0, 0);
L1.addView(FrameLayout,Param);

Param.addRule(RelativeLayout.CENTER_HORIZONTAL);
FrameLayout.addView(TL,Param);
RelativeParam.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

Drawable drawable = getResources().getDrawable(R.drawable.snail_menu); 
L1.setBackgroundDrawable(drawable);

this.setContentView(L1);