在android中如何在水平滚动视图中动态加载json图像和文本?
我有一个xml,它在水平滚动视图的相对布局中包含一个图像和文本视图…如何将来自服务器的图像和文本加载到水平滚动视图中在android中如何在水平滚动视图中动态加载json图像和文本?,android,json,Android,Json,我有一个xml,它在水平滚动视图的相对布局中包含一个图像和文本视图…如何将来自服务器的图像和文本加载到水平滚动视图中 <?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="fi
<?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获取图像和文本,我需要在水平滚动视图中填充它们…我在google中搜索了很多,但没有找到解决方案…请帮助 更新您的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();
对于(int i=0;我不知道您面临的问题是什么!!只需将图像的位图设置为ImageView,将文本设置为TextView,然后查找ViewById(R.id.horizontalScrollView1)。requestLayout()要刷新水平滚动视图,json中的图像和文本的数量以前是未知的…在这种情况下,我如何才能将它们加载到imageview中..你能告诉我一个例子吗谢谢你的回答..我是android新手,你能告诉我如何动态创建相对布局并添加到线性布局..你能不能我有一个示例代码,谢谢…但是我想在线性布局中增加相对布局之间的间距我怎么能做到…我想在两个相对布局之间增加10dp的间距…感谢帮助!我已经更新了anser…setMargin()如果这个答案有帮助,那么接受这个答案,接受这个答案,你能告诉我如何将一个图片url转换成图片吗,我得到的图片是url的形式,请帮我最后一个忙,我会很高兴的
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;
}
}