如何在同一RelativeLayout中添加多个视图?

时间:2013-12-05 08:32:40

标签: android android-layout

我的应用程序进展但是我遇到了问题。我可以使用我的函数setClassicLabel(JSONArray listLabel)在RelativeLayout中设置x TextView我在我的方法onCreate()中调用它并且它运行良好!但我有另一个功能setClassicImg(JSONArray listImg)。我必须在添加标签的相同布局中添加imageview。我试图在函数setClassicLabel(JSONArray listLabel)中创建一个RelativeLayout,在另一个函数中创建一个与ImageView对应的RelativeLayout。我的Json解析器运行良好,所以我不需要显示它的代码。

以下是我的 ClassicView.java 的代码:

public ClassicView() {

}
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        JSONParser jParser = null;
        try {
            jParser = new JSONParser("Json.txt");
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        content = (Content)getIntent().getSerializableExtra("CONTENT");


         try {
            //this.setClassicLabel(jParser.getContentViewWithId(content.getElemView()).getJSONArray("Label"));
            this.setClassicImg(jParser.getContentViewWithId(content.getElemView()).getJSONArray("Img"));
        //this.setClassicLabel(content.getJSONArray("Label"));
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void setClassicLabel(JSONArray listLabel) throws JSONException {
        RelativeLayout rl = new RelativeLayout(this);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        setContentView(rl);

        for (int i = 0; i < listLabel.length(); i++) {  
            TextView tv = new TextView(this);
            tv.setText(listLabel.getJSONObject(i).getString("text"));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (metrics.widthPixels * listLabel.getJSONObject(i).getDouble("size_x")), (int) (metrics.heightPixels * listLabel.getJSONObject(i).getDouble("size_y")));
            params.leftMargin = (int) (metrics.widthPixels * listLabel.getJSONObject(i).getDouble("position_x"));
            params.topMargin = (int) (metrics.heightPixels * listLabel.getJSONObject(i).getDouble("position_y"));
            rl.addView(tv, params);

        }
    }

    public void setClassicImg(JSONArray listImg) throws JSONException {
        RelativeLayout rl2 = new RelativeLayout(this);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        setContentView(rl2);

        for (int i = 0; i < listImg.length(); i++) {
            ImageView imgView = new ImageView(this);
            Log.e("IMG VIEW =", listImg.getJSONObject(i).getString("path"));
            bitmap = getBitmapFromUrl(listImg.getJSONObject(i).getString("path"));
            imgView.setImageBitmap(bitmap);
            RelativeLayout.LayoutParams paramsImage = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        /*  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams((int) (metrics.widthPixels * listImg.getJSONObject(i).getDouble("size_x")), (int) (metrics.heightPixels * listImg.getJSONObject(i).getDouble("size_y")));
            params.leftMargin = (int) (metrics.widthPixels * listImg.getJSONObject(i).getDouble("position_x"));
            params.topMargin = (int) (metrics.heightPixels * listImg.getJSONObject(i).getDouble("position_y")); */
            rl2.addView(imgView, paramsImage);
}

    public 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 (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

当我调用方法setClassicImg(JSONArray listImg)并且我不明白为什么时,屏幕上什么都没有显示。我有我的网址来获取我的ImageView,但我无法在我的设备上看到它。只有一个空的RelativeLayout。有人知道如何将所有元素(标签,imageview)放在同一个布局中?可能是我做错了..

标签对应于TextView,Img对应于ImageView

如果你能帮助我,谢谢你:)

2 个答案:

答案 0 :(得分:0)

我认为你得到了异常,但你只使用e.printStackTrace()来处理它。您正在尝试在UI线程中下载图像,这会引发系统异常。使用AsyncTask将下载移至后台线程,并确保正确下载了Bitmap,而不是返回null。

请参阅This

答案 1 :(得分:0)

您正在主线程上通过URL下载图像。它将抛出一个安全例外。在较新版本的android中,网络操作不支持主线程。您应该创建一个用于下载图像的异步任务。

    public synchronized void DownloadTask() {
    task1 = new AsyncTask<Object, Object, Object>() {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

        }

        @Override
        protected Object doInBackground(Object... objects) {

            // download your bitmap here
        }

        @Override
        protected void onPostExecute(Object o) {
            assign it to your ImageView here and add it to Relative layout
        }
    }.execute();

}