如何在Android的Horizo​​ntal Scrollview中动态加载json图像和文本?

时间:2015-01-27 07:14:57

标签: android json

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

<?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获取图像和文本我需要在水平滚动视图中填充它们...我在谷歌搜索了很多但是无法提出解决方案..请帮忙!

1 个答案:

答案 0 :(得分:1)

更新你的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;
}
}
  

注意: - 我没有测试过myside的代码。但逻辑是一样的。