【问题标题】:Set ImageView max height using code使用代码设置 ImageView 最大高度
【发布时间】:2015-12-17 11:33:30
【问题描述】:

我想将Max Height 设置为Imageview,它可以在加载图像时具有。最初我在XML 中设置了android:height="wrap_content",以便为其他视图腾出空间。但是在加载图像后,有时它会覆盖整个手机屏幕,我看不到其他视图,我想将高度限制在某个特定值。请!!任何帮助表示赞赏.. 干杯

【问题讨论】:

  • 您可以通过编程方式更改其大小。我应该举个例子吗?
  • 我已经务实地更改了布局大小,但没有尝试更改 imageview 大小..
  • imageview.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, YOUR_HEIGHT));
  • @AshishGaurav 示例总是受欢迎的.. :)
  • @Sunny 我可以更改布局大小,我现在想更改 ImageView 大小.. 如果应用于 imageview,此代码会出错

标签: android xml imageview


【解决方案1】:

描述有关显示的一般信息的结构,例如 它的大小、密度和字体缩放。

     DisplayMetrics metrics = getResources().getDisplayMetrics();

        int DeviceTotalWidth = metrics.widthPixels;
        int DeviceTotalHeight = metrics.heightPixels;


       /*
        *
        * Adding  Height Respect To Device
        * */

     ImageView  ImageViewObj=(ImageView)findViewById(R.id.ImageViewId);
     ImageViewObj.getLayoutParams().height= (int) (DeviceTotalHeight);

【讨论】:

    【解决方案2】:

    获取屏幕高度,并用它来设置一些比例,就像 Sunny 说的那样。

    /* get the screen height in pixels */
    public static int getScreenHeight(WindowManager windowManager) {
        Display display = windowManager.getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);
        return point.y;
    }
    /* take an imageview, and set its height to desired float ratio */
    public static void setImageHeight(Activity activity, ImageView imageView, float ratio) {
        int h = getScreenHeight(activity.getWindowManager());
        imageView.getLayoutParams().height = (int)(h*ratio);
    }
    

    然后在onCreate(....),试试做

    ImageView imageView = (ImageView) findViewById(R.id.whatever);
    setImageHeight(imageView, 0.4f); /* 40% */
    

    【讨论】:

      猜你喜欢
      • 2019-06-25
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      • 2011-05-15
      • 1970-01-01
      • 2019-01-21
      • 2017-07-30
      • 1970-01-01
      相关资源
      最近更新 更多