根据Button背景图片长宽比调整Button大小
2014-12-12 15:52
253 查看
需求:Button宽度为match_parent,因此宽度不定,但要根据背景图片的长宽比来调整Button大小使其适应背景图。
之前采用的方法是OnGlobalLayoutListener, 不过这样会导致同一个页面的EditText在修改文字时不改变文字。
Button completeButton = (Button)findViewById(R.id.completeButton);
completeButton.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
public void onGlobalLayout()
{
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.button_long);
double ratio = ((double)bitmap.getHeight()) / bitmap.getWidth();
ViewGroup.LayoutParams params = completeButton.getLayoutParams();
params.height = (int)(completeButton.getWidth() * ratio);;
completeButton.setLayoutParams(params);
}
});
但如果宽度固定,左右两侧的padding也已经知道,可以动态设置LayoutParams
DisplayMetrics metrics = getResources().getDisplayMetrics();
int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, metrics);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.button_long);
double ratio = ((double)bitmap.getHeight()) / bitmap.getWidth();
ViewGroup.LayoutParams params = <span style="font-family: Arial, Helvetica, sans-serif;">completeButton</span><span style="font-family: Arial, Helvetica, sans-serif;">.getLayoutParams();</span>
params.width = metrics.widthPixels - padding * 2;
params.height = (int)(params.width * ratio);<pre name="code" class="java">completeButton.setLayoutParams(params);
这样就可以了!
之前采用的方法是OnGlobalLayoutListener, 不过这样会导致同一个页面的EditText在修改文字时不改变文字。
Button completeButton = (Button)findViewById(R.id.completeButton);
completeButton.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
public void onGlobalLayout()
{
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.button_long);
double ratio = ((double)bitmap.getHeight()) / bitmap.getWidth();
ViewGroup.LayoutParams params = completeButton.getLayoutParams();
params.height = (int)(completeButton.getWidth() * ratio);;
completeButton.setLayoutParams(params);
}
});
但如果宽度固定,左右两侧的padding也已经知道,可以动态设置LayoutParams
DisplayMetrics metrics = getResources().getDisplayMetrics();
int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, metrics);
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.button_long);
double ratio = ((double)bitmap.getHeight()) / bitmap.getWidth();
ViewGroup.LayoutParams params = <span style="font-family: Arial, Helvetica, sans-serif;">completeButton</span><span style="font-family: Arial, Helvetica, sans-serif;">.getLayoutParams();</span>
params.width = metrics.widthPixels - padding * 2;
params.height = (int)(params.width * ratio);<pre name="code" class="java">completeButton.setLayoutParams(params);
这样就可以了!
相关文章推荐
- java 添加背景图片(可根据窗口大小自动调整显示图片)
- 根据桌面调整窗体大小和背景图片
- Android根据Button状态(normal,focused,pressed)显示不同背景图片
- java swing 怎样添加背景图片并且能根据窗口大小进行缩放
- Android根据Button状态(normal,focused,pressed)显示不同背景图片
- UIButton背景图片调整大小
- 手写NSButton设置的背景图片为什么不会随着Button的大小进行伸缩变换?
- 设置窗体背景图片,并且让图片随着窗体的大小的调整而调整大小
- Android根据Button状态(normal,focused,pressed)显示不同背景图片
- android之ImageButton实现选中改变背景,附加(图片自动适配组件大小)
- Excel VBA 根据合并单元格大小调整图片大小
- java中button背景图片大小设置,JFrame.getContentPane().add(pane)与JFrame.add(pane)区别
- Android根据Button状态(normal,focused,pressed)显示不同背景图片
- android 根据button不同状态显示不同(背景)图片(background!!!!!)
- 固定图片背景的按钮调整大小
- CSS控制背景图片随网页或容器自动调整大小
- Android根据Button状态(normal,focused,pressed)显示不同背景图片
- Android根据Button状态(normal,focused,pressed)显示不同背景图片
- Android根据Button状态(normal,focused,pressed)显示不同背景图片
- 根据size调整图片背景