gpt4 book ai didi

java - 动态添加ImageButtons

转载 作者:行者123 更新时间:2023-12-01 11:16:03 25 4
gpt4 key购买 nike

网络调用完成后,我必须为图像数组动态创建 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但我有点困惑,因为我试图根据网络请求设置图像。

谢谢

最佳答案

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

theViewGroupThatHoldsTheButtons.addView(new View(this));

您可能需要在新 View 上设置一些LayoutParams,或者您可以膨胀 View ,模仿如果您有一个列表和一个适配器。

关于java - 动态添加ImageButtons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31811325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com