【发布时间】: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 获得的位图不为空? (检查不花钱)