【问题标题】:Android SDK ImageButton stretches when Gravity.RightGravity.Right 时 Android SDK ImageButton 拉伸
【发布时间】:2012-02-10 01:21:18
【问题描述】:

我有这个代码:

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

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

那么在第一个代码sn-p中渲染图像的正确方法是什么,但是将按钮放在第二个代码sn-p放置的位置?

提前致谢。

【问题讨论】:

  • 试试 call.setScaleType(ScaleType.FIT_XY);

标签: android sdk imagebutton stretch gravity


【解决方案1】:

使用RelativeLayoutFrameLayout 代替LinearLayout

另见layout_gravity in LinearLayout

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

【讨论】:

  • 到目前为止,没有任何 cmets 或答案允许我做我想做的事情:将每个呼叫按钮右对齐,在 TableRow 中垂直居中每个按钮,同时防止按钮放大或拉伸。我查看了上面的示例,按照它进行操作后,我的按钮没有右对齐或垂直居中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多