簡體   English   中英

Android以編程方式創建的ImageView無法顯示

[英]Android programmatically created ImageView not showing

我正在嘗試以編程方式創建一個ImageViews數組。 這是我的代碼(i和j代表計數器)

imageViews = new ArrayList<ImageView>();
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            if(3*i+j != 8) {
                ImageView subImage = new ImageView(this);
                subImage.setImageBitmap(pieces.get(3 * i + j));
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(100, 100);
                subImage.setLayoutParams(lp);
                subImage.setX(i * 100f);
                subImage.setY(j * 100f);
                subImage.setVisibility(View.VISIBLE);
                imageViews.add(subImage);
            }
        }
    }

pieces是Bitmaps的一個arraylist。 無論如何,沒有任何圖像視圖顯示。 我對這一切都很陌生,所以我確定我做的各種各樣的錯誤。謝謝!

您必須在布局對象中添加視圖,如下所示。

layout_object.addView(subImage);

最后你必須設置你想要顯示的視圖

setContentView(layout_object);

希望這個答案對你有用。

你必須在xml文件中創建布局

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/linearLayout"
    android:orientation="vertical">

</LinearLayout>

並在java文件中

LinearLayout layout=(LinearLayout)view.findViewById(R.id.linearLayout);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                                android.widget.LinearLayout.LayoutParams.WRAP_CONTENT,
                                android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
    imageViews = new ArrayList<ImageView>();

        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 3; j++){
                if(3*i+j != 8) {
                    ImageView subImage = new ImageView(this);
                    subImage.setImageBitmap(pieces.get(3 * i + j));                        
                    subImage.setVisibility(View.VISIBLE);
                    subImage.setLayoutParams(params);
                    imageViews.add(subImage);

                    layout.addView(subImage);

                }
            }
        }

注意如果你有更多的圖像未在屏幕上設置,那么首先創建Scrollview,然后放置線性布局

你已經創建了所有的圖像視圖,但是你需要一個布局來顯示這些圖像。 讓我們說一個LinearLayout,所以首先在你的xml文件中創建一個布局,

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/myLinearLayout"
    android:orientation="vertical">

</LinearLayout>

現在,在此布局中添加圖像。

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.myLinearLayout);
imageViews = new ArrayList<ImageView>();
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            if(3*i+j != 8) {
                ImageView subImage = new ImageView(this);
                subImage.setImageBitmap(pieces.get(3 * i + j));
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(100, 100);
                subImage.setLayoutParams(lp);
                subImage.setX(i * 100f);
                subImage.setY(j * 100f);
                subImage.setVisibility(View.VISIBLE);
                imageViews.add(subImage);

            }
        }
    }

for(int i=0; i< imageViews.size(); i++){ // iterating through the arraylist and adding the images to the linearlayout
    mylinearLayout.addView(imageViews.get(0));
}

你有imageview對象的arraylist ..只需迭代這個數組列表並添加到主父布局

假設您的主要布局是RelativeLayout

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainlayout">
</LinearLayout>

在java代碼中保存此對象

RelativeLayout rl=(RelativeLayout)findViewById(R.id.mainlayout);
//now iterate the arraylist of imageviews
for(ImageView iv:imageViews)
{
    rl.addView(iv);
}

暫無
暫無

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

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