簡體   English   中英

動態布局中imageview的寬度和高度

[英]width and height of imageview in dynamic layout

我正在使用動態布局,我想設置ImageView寬度和高度。 代碼如下。 如何在給定的布局中設置ImageView的高度和寬度? 非常感謝您的幫助。

    RelativeLayout layout = new RelativeLayout(mContext);
    TextView text1 = new TextView(mContext);
    text1.setText(Constants.vctrCategory.elementAt(counter).toString());
    LayoutParams params1 = new    LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    text1.setLayoutParams(params1);
    text1.setTextSize(20);
    layout.addView(text1);
    ImageView image = new ImageView(mContext);
    image.setImageBitmap(bitmap);
    layout.addView(image);
    counter++;
    return layout;

可以只使用ImageView調用call addView() ,您可以使用同樣需要兩個整數作為寬度和高度的版本:

addView(查看子級,int width,int height)

另外,您可以直接在視圖的LayoutParams上設置寬度和/或高度,即在您自己的代碼段中調用layout.addView(image)之后:

layout.addView(image);
layout.getLayoutParams().width = width;
layout.getLayoutParams().height = height;

基本上,它類似於您已經為TextView所做的工作。

我設置了圖像視圖,但它不顯示

嘗試這樣的事情:

RelativeLayout layout = new RelativeLayout(this);

LayoutParams params1 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
TextView text1 = new TextView(this);
text1.setText(Constants.vctrCategory.elementAt(counter).toString());
text1.setTextSize(20);
layout.addView(text1, params1);

LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
ImageView image = new ImageView(this);
image.setImageBitmap(bitmap);
layout.addView(image, params2);

count++;
return layout;

還有很多其他LayoutParams類,請確保您導入RelativeLayout.LayoutParams。 如果仍然“不顯示”,則bitmap可能是罪魁禍首。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM