回覆列表
  • 1 # 使用者1842385614551315

    效果:layout介面佈局:[html] view plaincopyprint?<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:ad="http://schemas.android.com/apk/res/android"ad:layout_width="match_parent"ad:layout_height="match_parent"ad:orientation="vertical" ><LinearLayoutad:layout_width="match_parent"ad:layout_height="30dp"ad:background="@drawable/titlebar_bg"ad:orientation="horizontal" ><ImageViewad:layout_width="wrap_content"ad:layout_height="wrap_content"ad:src="@drawable/back_44_44" /><LinearLayoutad:layout_width="match_parent"ad:layout_height="30dp"ad:gravity="center" ><TextViewad:layout_width="wrap_content"ad:layout_height="wrap_content"ad:text="課程列表"ad:textSize="20sp" /></LinearLayout></LinearLayout><ScrollViewad:id="@+id/ScrollView"ad:layout_width="fill_parent"ad:layout_height="wrap_content"ad:scrollbars="vertical" ><LinearLayoutad:id="@+id/mainLayout"ad:layout_width="match_parent"ad:layout_height="wrap_content"ad:orientation="vertical" ></LinearLayout></ScrollView></LinearLayout>httputil輔助類:[java] view plaincopyprint?package com.tudou.activity.work4;import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import android.util.Log;public class HttpUtil {/*** 獲取到流,自己處理資料* @param path* @return*/public static InputStream getInputStream(String path) {HttpURLConnection conn = null;try {URL url = new URL(path);conn = (HttpURLConnection) url.openConnection();conn.setDoInput(true);// 設定是否向httpUrlConnection輸出,post請求,引數要放在http正文內conn.setDoOutput(true);conn.setReadTimeout(3000);conn.setConnectTimeout(3000);conn.setUseCaches(false);conn.setRequestMethod("POST");if (conn.getResponseCode() == 200) {Log.d("mylog", "getResponseCode:" + 200);return conn.getInputStream();}} catch (IOException e) {e.printStackTrace();} finally {if (conn != null) {conn.disconnect();}}return null;}/*** 直接返回響應體正文* @param path* @return*/public static String getResponseBody(String path,String params) {HttpURLConnection conn = null;StringBuffer result=new StringBuffer();try {URL url = new URL(path);conn = (HttpURLConnection) url.openConnection();conn.setDoInput(true);// 設定是否向httpUrlConnection輸出,post請求,引數要放在http正文內conn.setDoOutput(true);conn.setReadTimeout(3000);conn.setConnectTimeout(3000);conn.setUseCaches(false);conn.setRequestMethod("POST");//資料輸出流,該語句隱含的執行connect動作if(params!=null){DataOutputStream out = new DataOutputStream( conn.getOutputStream());//將引數寫入流,重新整理提交關閉流out.writeBytes(params);out.flush();out.close();}//讀取連線返回的資料BufferedReader reader = new BufferedReader(new InputStreamReader( conn.getInputStream()));String inputLine = null;while (((inputLine = reader.readLine()) != null)) {result.append(inputLine);//"\n";}//關閉reader.close();if (conn.getResponseCode() == 200) {Log.d("mylog", "getResponseCode:" + 200);}} catch (IOException e) {e.printStackTrace();} finally {if (conn != null) {conn.disconnect();}}return result.toString();}}主activity:[java] view plaincopyprint?package com.tudou.activity.work4;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import com.tudou.activity.R;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.util.Log;import android.util.TypedValue;import android.view.Gravity;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.LinearLayout.LayoutParams;import android.widget.ProgressBar;import android.widget.TextView;public class HomeWork4 extends Activity {String path = "此處省略,你要請求的地址";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.homework4);LinearLayout mainLayout = (LinearLayout)findViewById(R.id.mainLayout);String result=HttpUtil.getResponseBody(path,null);// Log.d("mylog", "result:" + result);try{JSONObject obj = new JSONObject(result);JSONArray array=obj.getJSONArray("onlineCourses");for (int i = 0; i < array.length(); i++) {JSONObject course= array.getJSONObject(i);// Log.d("mylog", "course:" + course.toString());//新增左邊的layoutLinearLayout leftlayout=new LinearLayout(this);//注意包android.widget.LinearLayout.LayoutParams,其它包下面的LayoutParams不起作用LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);params.topMargin=10;leftlayout.setLayoutParams(params);leftlayout.setOrientation(LinearLayout.HORIZONTAL);leftlayout.setGravity(Gravity.CENTER_VERTICAL);//新增左邊layout的圖片ImageView imageView=new ImageView(this);params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);imageView.setLayoutParams(params);imageView.setImageResource(R.drawable.image_default_195_130);leftlayout.addView(imageView);//新增右邊的layout,分為上下2部分,上面是標題,下面是進度條LinearLayout rightlayout=new LinearLayout(this);LayoutParams rightLayoutParams=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);rightlayout.setLayoutParams(rightLayoutParams);rightlayout.setOrientation(LinearLayout.VERTICAL);//新增課程標題TextView textView=new TextView(this);params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);textView.setLayoutParams(params);textView.setText(course.get("courseName").toString());textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);//18SP//學分,水平佈局,分為左右,左:學分,,右:分值LinearLayout studyLayout=new LinearLayout(this);params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);studyLayout.setLayoutParams(params);studyLayout.setOrientation(LinearLayout.HORIZONTAL);//新增學分TextView studyView=new TextView(this);params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);studyView.setLayoutParams(params);studyView.setText("學分:");studyView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);studyView.setTextColor(Color.parseColor("#b6b6b6"));//第2種方法:setTextColor(Color.rgb(255, 255, 255));//新增學分值TextView studyValueView=new TextView(this);params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);studyValueView.setLayoutParams(params);studyValueView.setText(course.get("courseCredit").toString());studyValueView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);//進度條,水平佈局,分為左中右,左:學習進度,中:進度條,右:%值LinearLayout processLayout=new LinearLayout(this);params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);processLayout.setLayoutParams(params);processLayout.setOrientation(LinearLayout.HORIZONTAL);//新增學習進度TextView processtextView=new TextView(this);params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);processtextView.setLayoutParams(params);processtextView.setText("學習進度:");processtextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);processtextView.setTextColor(Color.parseColor("#b6b6b6"));//新增進度條ProgressBar bar=new ProgressBar(this,null,android.R.attr.progressBarStyleHorizontal);//指定進度條樣式params=new LayoutParams(150, ViewGroup.LayoutParams.WRAP_CONTENT);bar.setLayoutParams(params);bar.setMax(100);bar.setProgress(10);//新增%值TextView processvaluetextView=new TextView(this);params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);processvaluetextView.setLayoutParams(params);processvaluetextView.setText("10%");processvaluetextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);processvaluetextView.setTextColor(Color.parseColor("#b6b6b6"));//新增標題rightlayout.addView(textView);//新增學分studyLayout.addView(studyView);studyLayout.addView(studyValueView);rightlayout.addView(studyLayout);//新增進度條processLayout.addView(processtextView);processLayout.addView(bar);processLayout.addView(processvaluetextView);rightlayout.addView(processLayout);//新增左右邊leftlayout.addView(rightlayout);mainLayout.addView(leftlayout);}}catch(JSONException e){e.printStackTrace(); 

  • 中秋節和大豐收的關聯?
  • 你喜歡的古風句子有哪些?