嘿,我有一个 ImageButton
网格,它按显示宽度缩放并包含一个 PNG 文件作为图像。我像这样将整个东西添加到线性布局中:
public void createButtons(){
int buttonX = 9;
int buttonY = 9;
int size = 80;
int tag = 0;
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(900,900) );
layout.setPadding(1,1,1,1);
layout.setBackgroundColor(0xff00af00); //green
RelativeLayout ll = (RelativeLayout)findViewById(R.id.rel);
ll.addView(layout);
for(int x=0;x<buttonX;x++) {
TableRow tr = new TableRow(this);
for(int y=0;y<buttonY;y++) {
but[x][y] = new ImageButton(this);
but[x][y].setBackgroundColor(0xff0000af); //blue
but[x][y].setImageResource(R.drawable.buttonmask3);
but[x][y].setScaleType(ImageButton.ScaleType.CENTER_INSIDE);
tr.addView(but[x][y], height/10,height/10);
}
layout.addView(tr);
}
}
问题是,现在的布局如下所示:
而它应该看起来像:
(这是一个快速的 Photoshop,但你明白了,比例应该完全填满按钮)
我能对这些小图像做些什么?我尝试了 CENTER_INSIDE、FITXY 和所有其他 ScaleTypes,但到目前为止我并不走运:/
宽度
取自屏幕宽度(或横向高度)
buttonmask3.png 大约是 170*170 像素。
最佳答案
问题是 ImageButton
自然带有的填充。
添加
but[x][y].setPadding(0, 0, 0, 0);
连同
but[x][y].setScaleType(ImageButton.ScaleType.CENTER_CROP);
帮我解决了。
关于android - ImageButton 上的 ScaleType 导致小图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26369632/