0
我嘗試以編程方式在我的按鈕中添加背景圖像時出現了一些問題。 它在我的XML文件上效果很好,但編程上我的按鈕的背景圖像比正常的要大。我的意思是我的圖像是高:150像素,但是當我運行這個代碼時,它看起來更大。 我已經嘗試設置我的按鈕maxheight功能,但它不起作用。 順便說一句,按鈕是在一行內,並在TableLayout內的行(顯然)。Android按鈕背景問題
感謝您的幫助!
for(int i = 0; i < 3; i++){
TableRow tableRow = new TableRow(this);
tableRow.setId(0 + i);
tableRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 150));
Button btnProduct = new Button (this);
btnProduct.setId(10 + i);
btnProduct.setBackgroundResource(R.drawable.shoes1);
btnProduct.setHeight(150);
btnProduct.setWidth(161);
btnProduct.setText("View");
tableRow.addView(btnProduct);
mainTableLayout.addView(tableRow);
}
你應該發佈你的XML進行比較,但我猜你已經在你的XML中將你的按鈕設置爲'150dp'而不是'150px'。這將解釋差異。看到[這篇文章](http://stackoverflow.com/questions/4605527/converting-pixels-to-dp-in-android)將dp轉換爲px。 – kabuko