动态添加ImageButtons

时间:2015-08-04 13:56:56

标签: java android xml

我必须在网络调用完成后为图像数组动态创建ImageButtons。我目前使用硬编码的按钮数量,但按钮数量将在服务器上动态添加和删除。

XML代码在其下面作为其硬编码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal"
          android:weightSum="1"
          tools:background="@color/black"
android:id="@+id/dotw_list">

<ImageButton
    android:id="@+id/dotw_imageButton_1"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:adjustViewBounds="false"
    android:background="@drawable/layout_bg"
    android:padding="5dp"
    android:scaleType="centerInside"/>

<ImageButton
    android:id="@+id/dotw_imageButton_2"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:adjustViewBounds="false"
    android:background="@drawable/layout_bg"
    android:padding="10dp"
    android:scaleType="centerInside"/>

<ImageButton
    android:id="@+id/dotw_imageButton_3"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:adjustViewBounds="false"
    android:background="@drawable/layout_bg"
    android:padding="10dp"
    android:scaleType="centerInside"/>

<ImageButton
    android:id="@+id/dotw_imageButton_4"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:adjustViewBounds="false"
    android:background="@drawable/layout_bg"
    android:padding="10dp"
    android:scaleType="centerInside"/>

<ImageButton
    android:id="@+id/dotw_imageButton_5"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginBottom="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp"
    android:adjustViewBounds="false"
    android:background="@drawable/layout_bg"
    android:padding="10dp"
    android:scaleType="centerInside"/>
</LinearLayout>

以下是我用来硬编码的代码,但我需要在数组中有更多/更少的项目时动态更改

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

    // Get extra data included in the Intent
    String message = intent.getStringExtra("network_response");
    Log.d("receiver", "Got message: " + message);

    home = data.getHomeItem();


    try {
        for (int i = 0; i < home.dotwItemArray.size(); i++) {
            System.out.println("Size of DOTW Array for the home screen is " + home.dotwItemArray.size());

            DotwItem dotwItem = home.dotwItemArray.get(i);

            if (i == 0) {
                request.getImage(dotwItem.getThumbnailImageUrl(), button_dotw_1);
                System.out.println("dotwItem1 is set");
            }
            if (i == 1) {
                request.getImage(dotwItem.getThumbnailImageUrl(), button_dotw_2);
                System.out.println("dotwItem2 is set");
            }
            if (i == 2) {
                request.getImage(dotwItem.getThumbnailImageUrl(), button_dotw_3);
                System.out.println("dotwItem3 is set");
            }
            if (i == 3) {
                request.getImage(dotwItem.getThumbnailImageUrl(), button_dotw_4);
                System.out.println("dotwItem4 is set");
            }
        }

    } catch (Exception e) {
        System.out.println("Error is: " + e + " - Exception is it: " + e.getStackTrace()[2].getLineNumber());

    }
}
};

我这样做的原因是因为我不知道在网络调用完成之前我所获得的数组的长度。网络调用是在onCreate方法中启动的,正如您在onReceive方法中看到的那样,此方法在网络调用完成后启动。

我看了一下这个link from StackOverflow但我有点困惑,因为我试图根据网络请求设置图像。

由于

1 个答案:

答案 0 :(得分:1)

网络通话后,只需将ImageButtons添加到您的布局中即可。我之前做过类似的事情。像下面这样的代码应该可以工作。

theViewGroupThatHoldsTheButtons.addView(new View(this));

您可能需要在LayoutParams上设置一些new View,或者您可以对视图进行充气,模仿您在拥有列表和适配器时所做的事情。

相关问题