自定义对话混淆

时间:2014-02-27 06:12:50

标签: android user-interface layout

所以我正在制作一款游戏,人们可以随着它们的成长购买连锁酒店的股票。 我需要一个特殊的对话框,其内容会随着游戏的进展而动态变化,因此我使用AlertDialog作为基础,从头开始创建自定义对话框。

我的问题是这些:

  1. 我尝试使用dialogContainer布局调整LinearLayout.LayoutParams以获得更大的宽度。由于某种原因,对对话框的大小有0影响。有点奇怪。是什么原因引起的?我需要一种方法来保持高度和宽度固定,因为我希望内容最终滚动。

  2. (已删除其他问题)

  3. 这是我到目前为止所获得的代码:

    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
    

    请注意:我尽可能使用线性布局,因为我来自网络开发背景,非常精通使用 div 标签。 (基本上是线性布局)。

1 个答案:

答案 0 :(得分:0)

要设置对话框的确切大小,您应该使用WindowManager.LayoutParams作为对话框窗口,因为框架本身定义了它的默认大小。 有很多类似的问题,例如this onethis

基本上,您可以在show()之后添加以下来电:

AlertDialog dialog = buyStockDialog.show();

dialog.getWindow().setLayout(600, 400);