gpt4 book ai didi

android - 如何在android的Horizo​​ntal Scrollview中动态加载json图片和文本?

转载 作者:行者123 更新时间:2023-11-30 02:23:19 24 4
gpt4 key购买 nike

我有一个 xml,其中包含水平 ScrollView 中相对布局内的图像和 TextView ...我如何将来自服务器的图像和文本加载到水平 ScrollView 中

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

<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_marginTop="40dp" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<RelativeLayout
android:id="@+id/relative1"
android:layout_width="100dp"
android:layout_height="100dp"
>

<ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
/>

<TextView
android:id="@+id/text_wheels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true"
android:text=" Text" />
</RelativeLayout>


</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>

我从 json 获取图像和文本我需要在水平 ScrollView 中填充它们...我在谷歌中搜索了很多但无法找到解决方案...请帮忙!

最佳答案

更新您的 xml...您需要做的是在 LinearLayout 中动态创建 RelativeLayouts...没有。 RelativeLayout 的大小等于你的 json 的大小

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

<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_marginTop="40dp" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<!--<RelativeLayout
android:id="@+id/relative1"
android:layout_width="100dp"
android:layout_height="100dp"
>

<ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
/>

<TextView
android:id="@+id/text_wheels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/image"
android:layout_centerHorizontal="true"
android:text=" Text" />
</RelativeLayout>-->


</LinearLayout>
</HorizontalScrollView>

假设你的 json 是这样的[{"图像":"","文本":""},{"图像":"","文本":""},{"图像":"","文本":""},. ...]

JsonArray jsonArray=new JsonArray(<json>);
for(int i=0;i<jsonArray.length();i++)
{
ImageView imgView = new ImageView(this); //create imageview dynamically
imgView.setImageBitmap(<image bitmap>);
TextView textView = new TextView(this);//create textview dynamically
textView.setText(<text>);

RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams lp;
lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams lp1;
lp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp1.setMargins(0,0,10,0);
rl.setLayoutParams(lp1);
imgView.setLayoutParams(lp);
textView.setLayoutParams(lp);

rl.addView(imgView,);//add imageview to relativelayout
rl.addView(textView);//add textview to relativelayout
indViewById(R.id.linearLayout).addView(rl);//now finally add this relativelayout to linearLayout of horizontall scrollview
}

public static Bitmap getBitmapFromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
// Log exception
return null;
}
}

Note: - i haven't tested the code from myside.but the logic is same.

关于android - 如何在android的Horizo​​ntal Scrollview中动态加载json图片和文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28165111/

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