【问题标题】:How to change width of an ImageView in Android SDK如何在 Android SDK 中更改 ImageView 的宽度
【发布时间】:2013-09-05 20:14:11
【问题描述】:

所以我正在尝试学习如何使用 Android 应用程序,并且我有一个名为 (id)“img_BarHealth”的 ImageView,我将其初始化为“160dp”。

我制作了一个按钮并将其连接到此功能:

public void onBack(View view) {
    //Get link to the object.
    ImageView bar_Health = (ImageView) findViewById(R.id.img_BarHealth);

    //This is what I want done. (However this value is not accessable it seems)
    bar_Health.width = "80dp";
}

那么,有什么建议吗? =)

【问题讨论】:

标签: android imageview


【解决方案1】:

使用 getLayoutParams().width

ImageView bar_Health = (ImageView) findViewById(R.id.img_BarHealth);
    bar_Health.getLayoutParams().width = 80;

【讨论】:

    【解决方案2】:

    你必须调用getLayoutParams().width然后你需要转换dip to pixel size

    public int convertDipToPixels(float dips)
    {
        return (int) (dips * getResources().getDisplayMetrics().density + 0.5f);
    }
    

    一些工作将接近:

    bar_Health.getLayoutParams().width = convertDipToPixels(80);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2017-05-23
      • 2015-12-14
      • 1970-01-01
      • 2014-01-12
      • 2012-02-11
      • 1970-01-01
      相关资源
      最近更新 更多