Android SDK ImageButton 在 Gravity.Right 时拉伸
我有这样的代码:
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);
它会拉伸图像,弄乱纵横比,并使其模糊/像素化。
那么,像第一个代码片段中那样渲染图像,但将按钮放置在第二个代码片段放置的位置的正确方法是什么?
提前致谢。
I have this code:
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);
It renders the button the way I want it, but when I do this:
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);
It stretches the image, messing up the aspect ratio, and makes it blurry/pixelated.
So what is the correct way to render the image as in the first code snippet, but place the button where the second code snippet puts it?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
RelativeLayout
或FrameLayout
而不是LinearLayout
。另请参阅LinearLayout 中的layout_gravity。
这是一个教程:Android 中相对布局的视觉指南。
Use
RelativeLayout
orFrameLayout
instead ofLinearLayout
.See also layout_gravity in LinearLayout.
This is a tutorial: A Visual Guide to Relative Layouts In Android.