簡體   English   中英

如何使用ImageView創建LinearLayout並將其以編程方式添加到另一個LinearLayout

[英]How to create LinearLayout with ImageView and add it to another LinearLayout programatically

我在嘗試以編程方式在內部創建帶有ImageView的LinearLayout時遇到麻煩(並將此LinearLayout添加到另一個LinearLayout)。 我嘗試重新創建的XML代碼是這樣的:

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/linearLayout_morty_combinationsV"
                    android:gravity="center">

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:background="@color/black"
                        android:layout_weight="1">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/morty"
                            android:id="@+id/imageView1"/>

                    </LinearLayout>

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:background="@color/black"
                        android:layout_weight="1">

                        <ImageView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/morty"
                            android:id="@+id/imageView2"/>

                    </LinearLayout>
                </LinearLayout>

這是我的Java代碼

        LinearLayout ll_combinations = (LinearLayout) findViewById(R.id.linearLayout_morty_combinationsV);
        Iterator<MortyObj> iterator = mortys.iterator();


        while (iterator.hasNext()) {
            final MortyObj mortyC = iterator.next();

            LinearLayout ll_new = new LinearLayout(this);
            ll_new.setOrientation(LinearLayout.VERTICAL);
            ll_new.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
            params.weight = 1f;
            ll_new.setLayoutParams(params);
            ll_new.setBackgroundColor(Color.WHITE);


            ImageView morty_imageview = new ImageView(this);

            try {
                InputStream ims = getAssets().open("morty_images/" + mortyC.getId() + ".png");
                Drawable d = Drawable.createFromStream(ims, null);
                morty_imageview.setImageDrawable(d);
            } catch (IOException e) {
                e.printStackTrace();
            }

            FrameLayout.LayoutParams imgParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            morty_imageview.setLayoutParams(imgParams);


            ll_new.addView(morty_imageview);
            ll_combinations.addView(ll_new);
          }

它應該是這樣的:

XML格式

這是它的外觀:

爪哇

最近兩個小時,我一直被困在這里。

您可以使用以下方式創建布局

View view = (View) findViewById(R.layout.current_layout); //the layout you set in `setContentView()`
LinearLayout picLL = new LinearLayout(CurrentActivity.this);
picLL.layout(0, 0, 100, 0);
picLL.setLayoutParams(new LayoutParams(1000, 60));
picLL.setOrientation(LinearLayout.HORIZONTAL);
((ViewGroup) view).addView(picLL);

您在layout()中傳遞的參數顯然取決於您想要的參數。 然后,您可以創建單獨的視圖以添加到剛創建的布局中。 但我強烈建議您通讀文檔,以了解可以在此處完成的所有操作。

編輯

ImageView myImage = new ImageView(this);
picLL.addView(myImage);
//set attributes for myImage;

剛發現問題。 如果我從res / drawables加載圖像,我會得到我想要的視圖,如果從資源加載圖像,它將不起作用

暫無
暫無

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

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