簡體   English   中英

如何制作4x4的imageviews網格?

[英]How do i make a 4x4 grid of imageviews?

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

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

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

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:src="@drawable/music" />

<ImageView
    android:id="@+id/imageView4"
    android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:src="@drawable/music" />

</TableLayout>

這是原始的XML,我只需要添加更多即可。 我有一個很大的imageView,因此需要縮小並將其復制到4x4網格中16次。 我只能在一列中顯示4張圖像

我不了解大圖像的問題,但我會告訴您我的建議:

有多種可能的解決方案:

  1. 由於您要創建16個imageView ,因此可以將GridViewBaseAdapter一起使用。 如果對在xml中看到它很重要,請使用isInEditMode作為custo GridView,然后將適配器設置為帶有假項目的適配器。 您應該意識到gridView上列/行的大小存在問題,尤其是在更改方向時。

  2. 另一種選擇是GridLayout

  3. 如果您堅持使用TableLayout,則可以有4個TableRow實例,每個實例的權重為1。 在每個視圖中,添加4個imageViews,每個視圖的權重為1。

添加4個TableRow並將您的ImageView放入其中。

或者,您可以通過代碼創建此網格。 像這樣:

LinearLayout container = null;
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1);
for (int i = 0; i < 16; ++i) {
    if (i % 4 == 0) {
        container = new LinearLayout(getActivity());
        container.setOrientation(LinearLayout.HORIZONTAL);
        mGrid.addView(container);
    }
    LinearLayout view = (LinearLayout) mLayoutInflater
        .inflate(R.layout.view_item, container, false);
        //populate the view in loop
        view.setLayoutParams(params);
        container.addView(view);
    }

暫無
暫無

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

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