【问题标题】:Center Image inside LinearLayout with weight在具有权重的 LinearLayout 中居中图像
【发布时间】:2014-04-14 12:19:42
【问题描述】:

这是我的问题的图片: 绿色和红色矩形中的两个小图像是我想要居中的图像。他们应该分别落在他们父母的中间。

这就是它不起作用的原因(据我所知): 我已将包装器的宽度设置为 0,以避免与为每个包装器设置的权重发生冲突。这让我可以在中间划分屏幕并在两侧划分内容。不幸的是,我认为这也意味着每张图像都试图将自己置于包装器的 0dp 部分而不是由权重生成的动态宽度上。

这是代码,你自己看看:

public void mergeHotels(Hotel keepHotel, Hotel absorbHotel, ArrayList<Hotel> absorbArray){
    Context context = getApplicationContext();

    //get the current player and let him choose what to do with stock first
    Player thisPlayer = players[getPlayerIndexByPlayOrder(CURRENT_TURN)];

    //create the dialog for exchanging stocks       
    ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.AppTheme);
    AlertDialog.Builder stockExchangeDialog = new AlertDialog.Builder(ctw);
    stockExchangeDialog.setTitle(getResources().getString(R.string.stock_exchange_dialog_title));

    LinearLayout dialogLayout = new LinearLayout(context);
    dialogLayout.setOrientation(LinearLayout.VERTICAL);

        LinearLayout dialogHeader = new LinearLayout(context);
        dialogHeader.setOrientation(LinearLayout.HORIZONTAL);
        dialogHeader.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 50, 1.0f));

            View blankSpace = new View(context);
            blankSpace.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 2.0f));

            LinearLayout tileOneWrapper = new LinearLayout(context);
            tileOneWrapper.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
            tileOneWrapper.setBackgroundColor(Color.GREEN);

                ImageView tileOne = new ImageView(context);
                String tileOneResourceName = "tile_" + keepHotel.getName().toLowerCase();
                int tileOneResourceId = getResources().getIdentifier(tileOneResourceName, "drawable", getPackageName());
                tileOne.setBackgroundResource(tileOneResourceId);
                LinearLayout.LayoutParams tileOneParams = new LinearLayout.LayoutParams(40, 40);
                tileOneParams.gravity = Gravity.CENTER;
                tileOne.setLayoutParams(tileOneParams);



            tileOneWrapper.addView(tileOne);

            LinearLayout tileTwoWrapper = new LinearLayout(context);
            tileTwoWrapper.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
            tileTwoWrapper.setBackgroundColor(Color.RED);

                ImageView tileTwo = new ImageView(context);
                String tileTwoResourceName = "tile_" + absorbHotel.getName().toLowerCase();
                int tileTwoResourceId = getResources().getIdentifier(tileTwoResourceName, "drawable", getPackageName());
                tileTwo.setBackgroundResource(tileTwoResourceId);
                LinearLayout.LayoutParams tileTwoParams = new LinearLayout.LayoutParams(40, 40);
                tileTwoParams.gravity = Gravity.CENTER;
                tileTwo.setLayoutParams(tileTwoParams);

            tileTwoWrapper.addView(tileTwo);

        dialogHeader.addView(blankSpace);
        dialogHeader.addView(tileOneWrapper);
        dialogHeader.addView(tileTwoWrapper);

        LinearLayout dialogBody = new LinearLayout(context);
        dialogBody.setOrientation(LinearLayout.VERTICAL);   

        for (int i = 0; i < players.length; i++) {
            LinearLayout playerStockDetails = new LinearLayout(context);
            playerStockDetails.setOrientation(LinearLayout.HORIZONTAL);

                TextView playerName = new TextView(context);
                playerName.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

                TextView playerMoney = new TextView(context);
                playerMoney.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

                TextView playerTileOneQuantity = new TextView(context);
                playerTileOneQuantity.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
                playerTileOneQuantity.setBackgroundColor(Color.LTGRAY);

                TextView playerTileTwoQuantity = new TextView(context);
                playerTileTwoQuantity.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
                playerTileTwoQuantity.setBackgroundColor(Color.CYAN);

                playerName.setText(players[i].getName());
                playerMoney.setText(String.valueOf(players[i].getMoney()));
                playerTileOneQuantity.setText(String.valueOf(players[i].getStockQuantityByCode(getHotelCodeByName(keepHotel.getName()))));
                playerTileTwoQuantity.setText(String.valueOf(players[i].getStockQuantityByCode(getHotelCodeByName(absorbHotel.getName()))));

            playerStockDetails.addView(playerName);
            playerStockDetails.addView(playerMoney);
            playerStockDetails.addView(playerTileOneQuantity);
            playerStockDetails.addView(playerTileTwoQuantity);
            playerStockDetails.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

            dialogBody.addView(playerStockDetails);
        }

    dialogLayout.addView(dialogHeader);
    dialogLayout.addView(dialogBody);       

    stockExchangeDialog.setView(dialogLayout);
    stockExchangeDialog.show();
}

那么如何让它基于动态宽度(基于权重)而不是我在 LayoutParams 中声明的静态宽度来居中图像?

提前感谢您的帮助!

JRadTheBad

【问题讨论】:

  • @Lokesh,这实际上是无关的。我的问题正在发生,因为我使用重量来决定父母的宽度。为了做到这一点,我必须将父级的静态宽度强制为零,然后设置权重。该解决方案不起作用。即使看起来完全不相关,Hariharan 也很准确。

标签: android image user-interface gravity


【解决方案1】:

试试这个..

将此行添加到您的LinearLayout LayoutParams

tileOneWrapper.gravity = Gravity.CENTER;

还有

tileTwoWrapper.gravity = Gravity.CENTER;

【讨论】:

  • 我明白你在做什么。因为图像在宽度为 0 的包装器上居中,所以将包装器居中在其自身的动态宽度内会强制图像居中。让我试试看。
  • 好电话@Hariharan。很抱歉您的答案被提前驳回。乍一看,它看起来完全不相关。这是一个不寻常的解决方案。本质上,它强制 0dp 布局视图以它自己的动态宽度居中,从而绕过任何尝试将图像本身“居中”的需要。感谢您的快速回答!
  • @Lokesh 他只为tileTwoParams ImageView Params 而不是LinearLayout
  • @JRadtheBad 您是否尝试过只为tileTwoParams ImageView Params 而不是LinearLayout
  • 它正在工作。我为 LinearLayout 设置了重力,它强制它在我想要的地方。谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-01
  • 1970-01-01
  • 2014-03-28
  • 1970-01-01
相关资源
最近更新 更多