在特定条件下隐藏按钮文字

时间:2018-10-29 15:13:09

标签: android

我目前正在开发一个需要从WAX ExpressTrade加载用户库存的应用程序。

我记下来了,现在正在向用户显示商品,但是我注意到了一些奇怪的干扰。

我有一个按钮,上面仅显示“添加项目”(用于添加您正在预览的项目到交易中)。直到我最近购买了新商品,此功能似乎也一直有效。

显示此新物品(Dual Berettas | Assassin)时,将显示按钮文字。

我已经调试了很多,发现:

  • 物品名称(某种程度上)与失踪有关。
  • 物品名称必须足够长,才能将物品条件包装到下一行(物品显示为“ Dual Berettas | Assassin(战痕累累)”)。
  • |后面不能超过2个空格。 (即,“ |”之后和括号之前的空格)。这意味着“双重Berettas |刺客测试(战痕累累)”将导致文本实际显示。

我希望我可以理解。

这是我显示商品的代码:

        final JSONObject item = new JSONObject(getIntent().getStringExtra("item"));
        LinearLayout layout = findViewById(R.id.item_layout);
        ImageView close = new ImageView(this);
        close.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_close_white_24dp, null));
        LinearLayout.LayoutParams closeParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        closeParams.gravity = Gravity.END;
        close.setLayoutParams(closeParams);
        close.setOnClickListener(new ImageView.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
        layout.addView(close);
        ImageView img = new ImageView(this);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        LinearLayout.LayoutParams imgParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        imgParams.leftMargin = 10;
        imgParams.rightMargin = 10;
        new ImageDownloader(img, 600, 600).execute(item.getJSONObject("image").getString("600px"));
        img.setLayoutParams(imgParams);
        layout.addView(img);
        if(item.has("in_offer") && item.getBoolean("in_offer")) {
            LinearLayout inOffer = new LinearLayout(this);
            inOffer.setOrientation(LinearLayout.HORIZONTAL);
            ImageView warning = new ImageView(this);
            warning.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_priority_high_white_24dp, null));
            inOffer.setBackgroundColor(Color.parseColor("#dc3545"));
            TextView inOfferText = new TextView(this);
            inOfferText.setText(R.string.in_offer);
            inOffer.setGravity(Gravity.CENTER_HORIZONTAL);
            inOffer.addView(warning);
            inOffer.addView(inOfferText);
            layout.addView(inOffer);
        }
        TextView name = new TextView(this);
        name.setText(item.getString("name"));
        name.setGravity(Gravity.CENTER_HORIZONTAL);
        layout.addView(name);
        if(item.has("wear")) {
            TextView wear = new TextView(this);
            wear.setText(getResources().getString(R.string.wear, item.get("wear").toString()));
            wear.setGravity(Gravity.CENTER_HORIZONTAL);
            layout.addView(wear);
        }
        TextView price = new TextView(this);
        double usdprice = ((double)item.getInt("suggested_price")) / 100.0;
        price.setText(getResources().getString(R.string.price, Double.toString(usdprice)));
        price.setGravity(Gravity.CENTER_HORIZONTAL);
        layout.addView(price);
        Button add = new Button(this);
        add.setBackgroundColor(Color.parseColor("#007bff"));
        LinearLayout.LayoutParams addParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        add.setLayoutParams(addParams);
        add.setPadding(100, 50, 100, 50);
        add.setGravity(Gravity.CENTER_HORIZONTAL);
        add.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("item", item.toString());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        add.setText(R.string.add_item);
        add.setTextSize(14);
        layout.addView(add);

请注意,名为img的ImageView的尺寸为600x600(我不知道这是否相关),如果不存在,文本也将正常显示。

在这里您可以看到文本在此项目(图片1)上显示得很好successful display,其中布局与失败的图片(图片2)上的高度完全相同failing display

1 个答案:

答案 0 :(得分:0)

删除add.setPadding(100, 50, 100, 50);并尝试建立agin。您的填充很可能会消失该按钮文本

如果与图像有关,则可以尝试滑行。它将解决该分辨率和性能问题。 https://github.com/bumptech/glide是存储库。

 GlideApp.with(this).load("https...").into(imageView);

也尝试