【问题标题】:How to add multiple views in the same RelativeLayout?如何在同一个RelativeLayout中添加多个视图?
【发布时间】:2013-12-05 17:01:56
【问题描述】:

我在我的应用程序中取得了进展,但我遇到了问题。我可以使用我的函数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) 时,屏幕上什么也没有出现,我不明白为什么。我有我的 url 来获取我的 ImageView,但我在我的设备上看不到它。只有一个空的RelativeLayout。有人知道我可以做些什么来将我的所有元素(标签、图像视图)放在同一个布局中?可能是我做错了什么..

label对应TextView,Img对应ImageView

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

【问题讨论】:

  • setClassicImg 中的 for 循环是否缺少右括号 }?
  • 不,右括号在我的代码中。但我无法将其粘贴到我的问题中。这不是问题
  • 您的所有图像都将被放置在彼此之上。你不应该为每个人指定位置吗?
  • 我试图指定位置。但我没有看到从 url 获得的图像..
  • 您是否检查过您从 URL 获得的位图不为空? (检查不花钱)

标签: android android-layout


【解决方案1】:

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

This

【讨论】:

    【解决方案2】:

    您正在从主线程上的 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();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-28
      • 1970-01-01
      • 2013-12-07
      相关资源
      最近更新 更多