【问题标题】:Setting the size of an Imageview in a dynamic Linearlayout在动态线性布局中设置 Imageview 的大小
【发布时间】:2012-09-02 08:24:01
【问题描述】:

我正在动态创建一个线性布局并向其中添加一些图像视图和文本视图。

代码

ScrollView sv = new ScrollView(this);

LinearLayout llay = new LinearLayout(this);

for(int i =0;i<ns;i++){
    TextView tv = new TextView(this);
    ImageView iv = new ImageView(this);
    iv.setLayoutParams(new LinearLayout.LayoutParams(100,100,50));

    llay.addView(tv);
    llay.addView(iv);
}

关于运行应用程序

第一个设备:图像视图大小合适。

第二个设备:图像视图太小。

有什么办法,我可以明确地设置图像视图的大小,使其变得独立于设备?

【问题讨论】:

    标签: android android-layout android-imageview


    【解决方案1】:

    试试这个....

    如果您的应用程序的不同尺寸密度使用显示高度/宽度而不是固定值。

    Display display = getWindowManager().getDefaultDisplay();
          int height = display.getHeight()/10;
          int width = display.getWidth()/5;
    
    ScrollView sv = new ScrollView(this);
    
    LinearLayout llay = new LinearLayout(this);
    
    for(int i =0;i<ns;i++){
        TextView tv = new TextView(this);
        ImageView iv = new ImageView(this);
        iv.setLayoutParams(new LinearLayout.LayoutParams(width,height,0));
    
        llay.addView(tv);
        llay.addView(iv);
    }
    

    【讨论】:

      【解决方案2】:

      建议使用 dp 而不是 px。

      有关 dp 与 px 的更多信息,您可以访问Support multiple screens

      在您的情况下,您以像素为单位设置值。如果您想使用 dp ,可以使用以下内容:

      float x = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
      

      【讨论】:

      • 您能否详细说明一下,这如何适用于我的情况?
      • 您当前的100 以像素为单位,您应该使用提供的方法将其更改为dp。
      猜你喜欢
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      相关资源
      最近更新 更多