簡體   English   中英

當添加到相對布局動態時,ImageView沒有邊距

[英]ImageView no margin when added to Relative Layout Dynamic

如果在我的相對布局中添加Imageview時遇到問題,如果將layoutparams添加到視圖中,則不會添加imageview,但是如果imageview沒有layoutparams,它將顯示但沒有圖像。 Imageview是通過代碼創建的。

TableLayout imageCon = (TableLayout)root.findViewById(R.id.home_screen_table_imagecon);
TableRow imageTableRow = new TableRow(root.getContext());
    TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

imageTableRow.addView(getLove());
imageTableRow.addView(getHappy());
imageCon.addView(imageTableRow,rowSpanLayout);
imageCon.setShrinkAllColumns(true);

private ImageView getLove(){
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    lp.setMargins(10, 10, 10,10);
    imgLove.setLayoutParams(lp);
    imgLove.setImageResource(R.drawable.img_emo_love);

    return imgLove;
}
private ImageView getHappy(){
    imgHappy.setImageResource(R.drawable.img_emo_happy);
    return imgHappy;
}

我期待着您的幫助。

謝謝Chkm8

您需要使用TableRow.LayoutParams ,因為您將ImageView添加到TableRow

LayoutParams更改為

TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,  TableRow.LayoutParams.MATCH_PARENT);
lp.setMargins(10, 10, 10,10);
imgLove.setLayoutParams(lp);

如果將ImageView添加到RelativeLayout則使用RelativeLayout.LayoutParams

如果將ImageView添加到LinearLayout則使用LinearLayout.LayoutParams

放在xml ImageView標記的行下面

android:adjustViewBounds="true"

請享用...

暫無
暫無

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

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