2

我面临的问题是:

  1. 我在线性布局中创建了一个动态按钮,垂直方向位于相对布局的中心。
  2. 然后,我想在线性布局的顶部添加一个图像,该图像显示在第一个按钮的右下角。

但是,在挣扎了2天之后,我仍然无法正确处理。“图像”显示在第一个按钮的中心。

我希望应用程序成为的图像。

在此处输入图像描述

这是特定的代码:

///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);

真的感谢您的帮助!

4

2 回答 2

0

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

于 2012-05-15T08:38:37.567 回答
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);

//在这里放置所有动态按钮 -vertical

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);
于 2012-05-15T09:50:27.193 回答