Android 自定义对话框混淆
所以我正在做一个游戏,人们可以在连锁酒店成长的过程中购买它们的股票。 我需要一个特殊的对话框,内容随着游戏的进行动态变化,所以我用AdvalCudio作为基础,从头开始创建一个自定义对话框。 我的问题是:Android 自定义对话框混淆,android,user-interface,layout,Android,User Interface,Layout,所以我正在做一个游戏,人们可以在连锁酒店成长的过程中购买它们的股票。 我需要一个特殊的对话框,内容随着游戏的进行动态变化,所以我用AdvalCudio作为基础,从头开始创建一个自定义对话框。 我的问题是: 我已尝试使用dialogContainer布局将LinearLayout.LayoutParams调整为更大的宽度。由于某些原因,is对对话框的大小没有影响。有点奇怪。知道是什么原因吗?我需要一种方法来保持高度和宽度固定,因为我希望内容最终滚动 (因其他问题而删除) 以下是迄今为止我对代码的了
public void buyStock(View view){
Player thisPlayer = players[getPlayerIndexByPlayOrder(CURRENT_TURN)];
Context context = getApplicationContext();
//generate content for dialog
LinearLayout dialogContainer = new LinearLayout(context);
dialogContainer.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(400, LinearLayout.LayoutParams.MATCH_PARENT, 1);
dialogContainer.setLayoutParams(params);
//title
TextView dialogTitle = new TextView(context);
dialogTitle.setText("Buy Stock:");
dialogTitle.setHeight(40);
dialogTitle.setWidth(600);
//each hotel stock options
//Text Content
Hotel testHotel = new Hotel("Tower", 0);
testHotel.setPrice(200);
View stockPicker = getStockPicker(testHotel);
dialogContainer.addView(dialogTitle);
dialogContainer.addView(stockPicker);
AlertDialog.Builder buyStockDialog = new AlertDialog.Builder(this);
buyStockDialog.setView(dialogContainer);
buyStockDialog.show();
}
public View getStockPicker(Hotel hotel){
Context context = getApplicationContext();
LinearLayout container = new LinearLayout(context);
container.setOrientation(LinearLayout.HORIZONTAL);
//Example color tile : image
ImageView tileBlock = new ImageView(context);
tileBlock.setBackgroundResource(tileResourceByHotel(hotel));
//Hotel Name
TextView hotelName = new TextView(context);
hotelName.setText(hotel.getName());
//Hotel Price
TextView hotelPrice = new TextView(context);
hotelPrice.setText("$" + hotel.getPrice());
//"Number-Picker"
LinearLayout numPicker = new LinearLayout(context);
numPicker.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, 400);
numPicker.setLayoutParams(params);
int textValue = 0;
//Up button
ImageView upArrow = new ImageView(context);
upArrow.setBackgroundResource(R.drawable.arrow_up);
//text
TextView pickerNum = new TextView(context);
pickerNum.setText(String.valueOf(textValue));
//down
ImageView downArrow = new ImageView(context);
upArrow.setBackgroundResource(R.drawable.arrow_down);
numPicker.addView(upArrow);
numPicker.addView(pickerNum);
numPicker.addView(downArrow);
container.addView(tileBlock);
container.addView(hotelName);
container.addView(hotelPrice);
container.addView(numPicker);
return container;
}
enter code here
请注意:我打算尽可能使用线性布局,因为我有web开发背景,并且非常精通使用div标记。(基本上是线性布局)。要设置对话框的确切大小,您应该使用for dialog window,因为框架本身定义了对话框的默认大小。 有很多类似的问题,比如或 基本上,您可以在
show()
之后添加以下调用:
这里似乎有点不对劲。getWindow()不是AlertDialog.Builder类的有效方法。它希望我能打出buyStockDialog,但它不能。还有其他想法吗?
AlertDialog dialog = buyStockDialog.show();
dialog.getWindow().setLayout(600, 400);