Android SDK ImageButton在Gravity.Right时延伸

时间:2012-02-09 16:06:55

标签: android sdk imagebutton stretch gravity

我有这段代码:

ImageButton call = new ImageButton(context);
call.setId(9001+result.index);
call.setBackgroundResource(R.drawable.small_call);
LinearLayout.LayoutParams call_params = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT
);
call.setLayoutParams(call_params);

按照我想要的方式呈现按钮,但是当我这样做时:

ImageButton call = new ImageButton(context);
call.setId(9001+result.index);
call.setBackgroundResource(R.drawable.small_call);
LinearLayout.LayoutParams call_params = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT,
    LinearLayout.LayoutParams.WRAP_CONTENT,
    Gravity.CENTER_VERTICAL | Gravity.RIGHT
);
call.setLayoutParams(call_params);

它会拉伸图像,弄乱纵横比,使其模糊/像素化。

那么在第一个代码片段中呈现图像的正确方法是什么,但是将按钮放在第二个代码片段所在的位置?

提前致谢。

1 个答案:

答案 0 :(得分:2)

使用RelativeLayoutFrameLayout代替LinearLayout

另见layout_gravity in LinearLayout

这是一个教程:A Visual Guide to Relative Layouts In Android