我正在尝试构建一个通用应用程序,我可以在线加载所有资源,因此根据输入,应加载某些资源。有可能吗?
答案 0 :(得分:1)
是的,可以在运行时从在线服务器加载图像和文本等资源。 你所要做的就是
致电您的网络服务
从回复中获取您的资源(我猜,文字和图片是您的要求)
夸大你的布局
设置他们的价值/资源。
编辑:
第3段指出:
出于业绩原因,观点通胀严重依赖 在构建时完成的XML文件的预处理。因此,它 目前无法将LayoutInflater与XmlPullParser一起使用 在运行时通过纯XML文件;它只适用于从编译资源(R.something文件)返回的XmlPullParser。
我没有提到在运行时创建yourLayout.xml,在这种情况下我会错的。
在此证明我的观点是该项目中我的一个班级的例子:
public class WriteScreen extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.writescreen);
title=(TextView)findViewById(R.id.writescreentitle);
array_listaudiodurationurl=new ArrayList<String>();
array_listcreatorname=new ArrayList<String>();
array_listtype=new ArrayList<String>();
array_listurl=new ArrayList<String>();
array_listcreatorid=new ArrayList<String>();
array_listcreatoravatar=new ArrayList<String>();
array_listid=new ArrayList<String>();
array_listcontent=new ArrayList<String>();
array_listduration=new ArrayList<String>();
array_listtype1=new ArrayList<String>();
array_listimagetype=new ArrayList<String>();
array_listimageurl=new ArrayList<String>();
array_listurl1=new ArrayList<String>();
array_listlastmodified=new ArrayList<String>();
array_listcreated=new ArrayList<String>();
array_listmaintype=new ArrayList<String>();
array_listsource=new ArrayList<String>();
array_listaudiourl=new ArrayList<String>();
Intent in =getIntent();
eId = in.getStringExtra("EventsId");
tit=in.getStringExtra("Title");
title.setText(tit);
mypref = PreferenceManager.getDefaultSharedPreferences(WriteScreen.this);
username = mypref.getString("username", "0");
password = mypref.getString("Password", "0");
write=(Button)findViewById(R.id.writescreenwritebtn);
moderate =(Button)findViewById(R.id.writescreenmoderate);
users=(Button)findViewById(R.id.writescreenusers);
try
{
try
{
writescreen();
}
catch (JSONException e)
{
e.printStackTrace();
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
writelowerparts();
}
public void playAudio()
{
if (WriteScreen.this.audioStreamer.getMediaPlayer().isPlaying())
{
WriteScreen.this.audioStreamer.getMediaPlayer().pause();
}
else
{
WriteScreen.this.audioStreamer.getMediaPlayer().start();
WriteScreen.this.audioStreamer.startPlayProgressUpdater();
}
isPlaying = !isPlaying;
}
public void writelowerparts()
{
boolean isgrey=true;
TableLayout tl = (TableLayout)findViewById(R.id.myTableLayoutwritescreen);
tl.setOrientation(LinearLayout.VERTICAL);
if (tl != null)
{
tl.removeAllViews();
}
if(array_listcreatorid.size() > 0 )
{
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LayoutParams lpAddRow = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LayoutParams lpImageView = new LayoutParams(MyEvents.width - (MyEvents.width/2), LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
for( i = 0; i < array_listcreatorid.size(); i++)
{
Calendar calendar = Calendar.getInstance();
int ro = calendar.getTimeZone().getRawOffset();
int dst = calendar.getTimeZone().getDSTSavings();
int startInd = array_listlastmodified .get(i).indexOf("(");
int EndInd = array_listlastmodified.get(i).indexOf("+") ;
String createdDate = array_listlastmodified.get(i).substring(startInd + 1, EndInd);
long created1 = Long.parseLong(createdDate) + ro + dst;
Date cDate = null;
SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss ");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));//TimeZone.getDefault());
String dateString = formatter.format(new Date(created1));
calendar.setTimeInMillis(created1);
calendar.setTimeZone(TimeZone.getDefault());
TableRow tr = new TableRow(this);
tr.setLayoutParams(lp);
View itemView = new View(this);
Bitmap bm = getBitmapFromUrl(array_listcreatoravatar.get(i));
if(array_listmaintype.get(i).equalsIgnoreCase("text"))
{
itemView = inflater.inflate(R.layout.writescreenlowerparts, null);
ImageView writescreenimageview=new ImageView(this);
TextView writescreenposttext = new TextView(this);
TextView writescreenusernametext = new TextView(this);
TextView writescreendatetext=new TextView(this);
TextView writescreensourcetext=new TextView(this);
writescreensourcetext=(TextView)itemView.findViewById(R.id.writescreensourcetext);
writescreendatetext=(TextView)itemView.findViewById(R.id.writescreendatetext);
writescreenposttext=(TextView)itemView.findViewById(R.id.writescreenposttext);
writescreenusernametext=(TextView)itemView.findViewById(R.id.writescreenusernametext);
LayoutParams param = new LayoutParams(MyEvents.width-90, LayoutParams.WRAP_CONTENT);
writescreenposttext.setLayoutParams(param);
if(array_listsource.get(i).equalsIgnoreCase("mobile"))
{
writescreensourcetext.setText(" via mobile");
}
else
{
writescreensourcetext.setText("");
}
writescreenposttext.setText(""+array_listcontent.get(i));
writescreenusernametext.setText(array_listcreatorname.get(i)+" at ");
int thisHours = calendar.getTime().getHours();
if (thisHours > 12)
{
thisHours = thisHours - 12;
}
String thisH = "" + thisHours;
if (thisHours == 0)
{
thisH = "00";
}
String amPM = calendar.get(Calendar.AM_PM)== 0?"AM":"PM";
if(calendar.getTime().getMinutes() < 10)
{
writescreendatetext.setText(thisH + ":0" + calendar.getTime().getMinutes() + " " + amPM);
}
else
{
writescreendatetext.setText(thisH + ":" + calendar.getTime().getMinutes() + " " + amPM);
}
Log.v("#############################",dateString );
Log.v("----------------------", array_listcreatorname.get(i));
writescreenimageview=(ImageView)itemView.findViewById(R.id.writescreenimageview);
writescreenimageview.setImageBitmap(bm);
if(isgrey==false)
{
TableRow tabttr1=new TableRow(this);
tabttr1.setBackgroundDrawable(getResources().getDrawable(R.drawable.topredline));
tabttr1.setLayoutParams(lp);
tl.addView(tabttr1, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
}
else if(array_listmaintype.get(i).equalsIgnoreCase("image"))
{
itemView= inflater.inflate(R.layout.writeimagelayout, null);
TextView writescreencontent =new TextView(this);
TextView writescreenimagedatetext =new TextView(this);
TextView writescreenusernameimagetext =new TextView(this);
ImageView writescreenimagetypeimage=new ImageView(this);
ImageView writescreenbigtypeimage=new ImageView(this);
TextView writescreenimagesourcetext =new TextView(this);
writescreenimagesourcetext=(TextView) itemView.findViewById(R.id.writescreenimagesourcetext);
if(array_listsource.get(i).equalsIgnoreCase("mobile"))
{
writescreenimagesourcetext.setText(" via mobile");
}
else
{
writescreenimagesourcetext.setText("");
}
writescreencontent=(TextView) itemView.findViewById(R.id.writecaption);
writescreenimagetypeimage=(ImageView)itemView.findViewById(R.id.writescreenimagetypeimage);
writescreenbigtypeimage=(ImageView)itemView.findViewById(R.id.writescreenbigtypeimage);
writescreenbigtypeimage.setTag(i);
if(array_listcontent.get(i).length() > 1)
{
writescreencontent.setText(array_listcontent.get(i));
}
writescreenbigtypeimage.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
int j = Integer.valueOf(v.getTag().toString());
Intent in=new Intent(WriteScreen.this,FullImage.class);
fullimage= array_listimageurl.get(j);
Log.v("%%%%%%%%%%%%%%%%%%%%%%%%%%%%",array_listimageurl.get(j));
startActivity(in);
}
});
writescreenusernameimagetext=(TextView)itemView.findViewById(R.id.writescreenusernameimagetext);
writescreenimagedatetext=(TextView)itemView.findViewById(R.id.writescreenimagedatetext);
writescreenusernameimagetext.setText(array_listcreatorname.get(i)+ " at ");
int thisHours = calendar.getTime().getHours();
if (thisHours > 12)
{
thisHours = thisHours - 12;
}
String thisH = "" + thisHours;
if (thisHours == 0)
{
thisH = "00";
}
String amPM = calendar.get(Calendar.AM_PM)== 0?"AM":"PM";
// myeventstime1.setText(cDate.toGMTString().substring(cDate.toGMTString().indexOf(":")-3).trim());
if(calendar.getTime().getMinutes() < 10)
{
writescreenimagedatetext.setText(thisH + ":0" + calendar.getTime().getMinutes() + " " + amPM);
}
else
{
writescreenimagedatetext.setText(thisH + ":" + calendar.getTime().getMinutes() + " " + amPM);
}
String s = array_listimageurl.get(i);
Bitmap bm1 = getBitmapFromUrl(s);
writescreenimagetypeimage.setImageBitmap(bm);
writescreenbigtypeimage.setLayoutParams(lpImageView);
writescreenbigtypeimage.setImageBitmap(bm1);
if(isgrey==false)
{
TableRow tabttr1=new TableRow(this);
tabttr1.setBackgroundDrawable(getResources().getDrawable(R.drawable.topredline));
tabttr1.setLayoutParams(lp);
tl.addView(tabttr1, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
}
else if(array_listmaintype.get(i).equalsIgnoreCase("video"))
{
itemView= inflater.inflate(R.layout.writescreenvideo, null);
ImageView writescreenimagesmallvideotype=new ImageView(this);
ImageView writescreenbigvideotype=new ImageView(this);
TextView writescreencontentvideotype =new TextView(this);
TextView writescreenusernamevideotype =new TextView(this);
TextView writescreendateandtimevideotype =new TextView(this);
TextView writescreensourcevideotype =new TextView(this);
writescreensourcevideotype=(TextView)itemView.findViewById(R.id.writescreensourcevideotype);
if(array_listsource.get(i).equalsIgnoreCase("mobile"))
{
writescreensourcevideotype.setText(" via mobile");
}
else
{
writescreensourcevideotype.setText("");
}
writescreenimagesmallvideotype=(ImageView)itemView.findViewById(R.id.writescreenimagesmallvideotype);
writescreenbigvideotype=(ImageView)itemView.findViewById(R.id.writescreenbigvideotype);
writescreencontentvideotype=(TextView)itemView.findViewById(R.id.writescreencontentvideotype);
writescreenusernamevideotype=(TextView)itemView.findViewById(R.id.writescreenusernamevideotype);
writescreendateandtimevideotype =(TextView)itemView.findViewById(R.id.writescreendateandtimevideotype);
writescreencontentvideotype.setText(array_listcontent.get(i));
writescreenusernamevideotype.setText(array_listcreatorname.get(i)+" at ");
int thisHours = calendar.getTime().getHours();
if (thisHours > 12)
{
thisHours = thisHours - 12;
}
String thisH = "" + thisHours;
if (thisHours == 0)
{
thisH = "00";
}
String amPM = calendar.get(Calendar.AM_PM)== 0?"AM":"PM";
if(calendar.getTime().getMinutes() < 10)
{
writescreendateandtimevideotype.setText(thisH + ":0" + calendar.getTime().getMinutes() + " " + amPM);
}
else
{
writescreendateandtimevideotype.setText(thisH + ":" + calendar.getTime().getMinutes() + " " + amPM);
}
writescreenimagesmallvideotype.setImageBitmap(bm);
String s1 = array_listurl1.get(i);
Bitmap bm2 = getBitmapFromUrl(s1);
writescreenbigvideotype.setLayoutParams(lpImageView);
writescreenbigvideotype.setImageBitmap(bm2);
writescreenbigvideotype.setTag(i);
writescreenbigvideotype.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int j = Integer.valueOf(v.getTag().toString());
Intent in=new Intent(WriteScreen.this,WatchVideo.class);
in.putExtra("path", array_listurl.get(j));
startActivity(in);
}
});
if(isgrey==false)
{
TableRow tabttr1=new TableRow(this);
tabttr1.setBackgroundDrawable(getResources().getDrawable(R.drawable.topredline));
tabttr1.setLayoutParams(lp);
tl.addView(tabttr1, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
}
else if(array_listmaintype.get(i).equalsIgnoreCase("audio"))
{
itemView= inflater.inflate(R.layout.writescreenaudio, null);
ImageView writescreenimagesmallaudiotype=new ImageView(this);
ImageButton writescreenbuttonaudioplay =new ImageButton(this);
TextView writescreencontentaudiotype =new TextView(this);
TextView writescreenusernameaudiotype =new TextView(this);
//TextView writescreendateandtimeaudiotype =new TextView(this);
ImageButton writescreenbuttonaudiostop =new ImageButton(this);
TextView writescreendurationaudiotype =new TextView(this);
TextView writescreendtimeaudiotype =new TextView(this);
TextView writescreensourceaudiotype =new TextView(this);
writescreenbuttonaudiostop=(ImageButton)itemView.findViewById(R.id.writescreenbuttonaudiostop);
writescreensourceaudiotype=(TextView) itemView.findViewById(R.id.writescreensourceaudiotype);
if(array_listsource.get(i).equalsIgnoreCase("mobile"))
{
writescreensourceaudiotype.setText(" via mobile");
}
else
{
writescreensourceaudiotype.setText("");
}
writescreendurationaudiotype=(TextView)itemView.findViewById(R.id.writescreendurationaudiotype);
writescreendurationaudiotype.setText(array_listaudiodurationurl.get(i));
writescreenbuttonaudioplay=(ImageButton)itemView.findViewById(R.id.writescreenbuttonaudioplay);
writescreenbuttonaudioplay.setTag(i);
writescreenbuttonaudioplay.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
int j = Integer.valueOf(v.getTag().toString());
String path=array_listaudiourl.get(j);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try
{
mediaPlayer.setDataSource(path);
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
mediaPlayer.prepare();
}
catch (IllegalStateException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer paramMediaPlayer)
{
mediaPlayer.stop();
}
});
}
});
writescreenbuttonaudiostop.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View paramView)
{
if(mediaPlayer!=null&& mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
}
});
writescreenimagesmallaudiotype=(ImageView)itemView.findViewById(R.id.writescreenimagesmallaudiotype);
writescreenimagesmallaudiotype.setImageBitmap(bm);
writescreencontentaudiotype=(TextView) itemView.findViewById(R.id.writescreencontentaudiotype);
writescreencontentaudiotype.setText(array_listcontent.get(i));
writescreenusernameaudiotype=(TextView) itemView.findViewById(R.id.writescreenusernameaudiotype);
writescreenusernameaudiotype.setText(array_listcreatorname.get(i)+" at ");
writescreenbuttonaudiostop=(ImageButton)itemView.findViewById(R.id.writescreenbuttonaudiostop);
writescreendtimeaudiotype=(TextView)itemView.findViewById(R.id.writescreendtimeaudiotype);
int thisHours = calendar.getTime().getHours();
if (thisHours > 12)
{
thisHours = thisHours - 12;
}
String thisH = "" + thisHours;
if (thisHours == 0)
{
thisH = "00";
}
String amPM = calendar.get(Calendar.AM_PM)== 0?"AM":"PM";
// myeventstime1.setText(cDate.toGMTString().substring(cDate.toGMTString().indexOf(":")-3).trim());
if(calendar.getTime().getMinutes() < 10)
{
writescreendtimeaudiotype.setText(thisH + ":0" + calendar.getTime().getMinutes() + " " + amPM);
}
else
{
writescreendtimeaudiotype.setText(thisH + ":" + calendar.getTime().getMinutes() + " " + amPM);
}
if(isgrey==false)
{
TableRow tabttr1=new TableRow(this);
tabttr1.setBackgroundDrawable(getResources().getDrawable(R.drawable.topredline));
tabttr1.setLayoutParams(lp);
tl.addView(tabttr1, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
}
tr.addView(itemView);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
isgrey=false;
}
}
}
public Drawable getDrawableFromUrl(String url)
{
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is,"img");
return d;
}
catch (Exception e) {
// TODO: handle exception
return null;
}
}
public Bitmap getBitmapFromUrl(String url)
{
HttpGet httpRequest = null;
httpRequest = new HttpGet(url);
Log.v("----------------------", url);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = null;
try
{
response = (HttpResponse) httpclient.execute(httpRequest);
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = null;
try
{
bufHttpEntity = new BufferedHttpEntity (entity);
}
catch (IOException e)
{
e.printStackTrace();
}
InputStream instream = null;
try
{
instream = bufHttpEntity.getContent();
}
catch (IOException e)
{
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(instream);
return bm;
}
public void writescreen() throws ClientProtocolException, IOException, JSONException
{
HttpClient client = new DefaultHttpClient();
String un = android.util.Base64.encodeToString(username.getBytes("UTF-8"),android.util.Base64.NO_WRAP);
String ps = android.util.Base64.encodeToString(password.getBytes("UTF-8"),android.util.Base64.NO_WRAP);
AuthScope as = new AuthScope(hostSecure,443);
UsernamePasswordCredentials upc = new UsernamePasswordCredentials(un,ps);
String auth = android.util.Base64.encodeToString((username + ":" + password).getBytes("UTF-8"),android.util.Base64.NO_WRAP);
((AbstractHttpClient) client).getCredentialsProvider() .setCredentials(as, upc);
BasicHttpContext localContext = new BasicHttpContext();
BasicScheme basicAuth = new BasicScheme();
localContext.setAttribute("preemptive-auth", basicAuth);
HttpHost targetHost = new HttpHost(hostSecure, 443, "https");
HttpGet httpget = new HttpGet("https://apiv1secure.somedomain.com/event/"+eId+"/page?Token=RCFB3m4W"+ "&format=json");
httpget.addHeader("Authorization", "Basic "+ auth);
HttpResponse response = client.execute(targetHost, httpget, localContext);
HttpEntity entity = response.getEntity();
Object content = EntityUtils.toString(entity);
Log.e("", "----------------------------------------"+content);
json = new JSONObject(content.toString());
JSONArray s = null;
s = json.getJSONArray("Posts");
for(int i = 0; i< s.length();i++)
{
String a = s.getString(i);
JSONObject jt = new JSONObject(a);
String postid=jt.getString("Id");
if(jt.has("Content"))
{
String postcontent=jt.getString("Content");
array_listcontent.add(postcontent);
}
else
{
array_listcontent.add("");
}
String creator = jt.getString("Creator");
array_listid.add(postid);
JSONObject jt1 = new JSONObject(creator);
String creatorid = jt1.getString("Id");
String creatoravatar=jt1.getString("Avatar");
String creatorname=jt1.getString("Name");
array_listcreatorname.add(creatorname);
array_listcreatorid.add(creatorid);
array_listcreatoravatar.add(creatoravatar);
mainType=jt.getString("Type");
array_listmaintype.add(mainType);
lastmodified=jt.getString("LastModified");
array_listlastmodified.add(lastmodified);
String createddate=jt.getString("Created");
array_listcreated .add(createddate);
source=jt.getString("Source");
array_listsource.add(source);
if(mainType.equalsIgnoreCase("image"))
{
String Media = "";
if(jt.has("Media"))
{
JSONArray mediatype = jt.getJSONArray("Media");
String d = mediatype.getString(0);
JSONObject jmediatype = new JSONObject(d);
d = mediatype.getString(0);
jmediatype = new JSONObject(d);
imagetype = jmediatype.getString("Type");
String imageurl=jmediatype.getString("Url");
array_listimagetype.add(imagetype);
array_listimageurl.add(imageurl);
array_listtype1.add("0");
array_listurl1.add("0");
array_listduration.add("0");
array_listurl.add("0");
array_listtype.add("0");
array_listaudiourl.add("0");
array_listaudiodurationurl.add("0");
}
}
else if (mainType.equalsIgnoreCase("video"))
{
String Media = "";
if(jt.has("Media"))
{
JSONArray mediatype= jt.getJSONArray("Media");
String d = mediatype.getString(0);
JSONObject jmediatype = new JSONObject(d);
String type=jmediatype.getString("Type");
String url=jmediatype.getString("Url");
String durationtype=jmediatype.getString("Duration");
array_listduration.add(durationtype);
array_listurl.add(url);
array_listtype.add(type);
d = mediatype.getString(1);
jmediatype = new JSONObject(d);
String type1 = jmediatype.getString("Type");
String url1=jmediatype.getString("Url");
array_listtype1.add(type1);
array_listurl1.add(url1);
array_listimagetype.add("0");
array_listimageurl.add("0");
array_listaudiourl.add("0");
array_listaudiodurationurl.add("0");
}
}
else if (mainType.equalsIgnoreCase("text"))
{
array_listimagetype.add("0");
array_listimageurl.add("0");
array_listtype1.add("0");
array_listurl1.add("0");
array_listduration.add("0");
array_listurl.add("0");
array_listtype.add("0");
array_listaudiourl.add("0");
array_listaudiodurationurl.add("0");
}
else if(mainType.equalsIgnoreCase("audio"))
{
JSONArray mediatype= jt.getJSONArray("Media");
String d = mediatype.getString(0);
JSONObject jmediatype = new JSONObject(d);
String audiourl=jmediatype.getString("Url");
String audioduration=jmediatype.getString("Duration");
array_listaudiourl.add(audiourl);
array_listaudiodurationurl.add(audioduration);
array_listimagetype.add("0");
array_listimageurl.add("0");
array_listtype1.add("0");
array_listurl1.add("0");
array_listduration.add("0");
array_listurl.add("0");
array_listtype.add("0");
}
}
}
答案 1 :(得分:0)
不,这是不可能的。 LayoutInflater
明确指出(第3段)。
但是,您可以从网络加载位图和文本并手动注入它们。但是,无法加载和扩充布局资源。