我的应用程序的布局是FrameLayout。我想将ImageButton对齐到framelayout的右上角。我尝试下面的代码没有用。
mImageButton = new ImageButton(mContext);
LayoutParams ll = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT );
ll.gravity = Gravity.RIGHT;
mImageButton.setLayoutParams(ll);
mImageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_menu_moreoverflow_normal_holo_light));
this.addView(mImageButton);
答案 0 :(得分:1)
我认为您应该将LayoutParams的构造代码更改为:
LayoutParams ll = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll.gravity = Gravity.TOP | Gravity.RIGHT;
在您的代码中,您将layout_width
和layout_height
值设置为MATCH_PARENT
,因此您的ImageView将填充所有父级FrameLayout。
答案 1 :(得分:1)
我认为您需要在布局参数中提供高度宽度。因为使用match_parent,它将占用父级的宽度和高度,或者您可以使用WRAP_CONTENT。
mImageButton = new ImageButton(mContext);
LayoutParams ll = new LayoutParams(width,height);
ll.gravity = Gravity.RIGHT | Gravity.TOP;
mImageButton.setLayoutParams(ll);
mImageButton.setImageDrawable(getResources().getDrawable(R.drawable.ic_menu_moreoverflow_normal_holo_light));
this.addView(mImageButton);
答案 2 :(得分:1)
试试这个,
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.TOP | Gravity.RIGHT;
mImageButton.setLayoutParams(params);