为什么我无法为relativeLayout对象设置自定义大小

时间:2015-09-04 17:21:19

标签: android

在以下代码中,为什么我无法为relativeLayout对象设置自定义大小?我的设备分辨率为240 * 320。

RelativeLayout relativeLayout;
    @Override
    protected void onCreate(Bundle bundle){
            super.onCreate(bundle);
    relativeLayout = new RelativeLayout(this);
    relativeLayout.getLayoutParams().width=240;
        relativeLayout.getLayoutParams().height=320;
    setContentView(relativeLayout);}

1 个答案:

答案 0 :(得分:1)

你应该尝试使用这样的布局参数:

public class ReadResult extends ListActivity {
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> ResultFetch;
private static String url_readResult = "http://10.0.2.2/Result-Viewer/php/ReadData.php";

private static final String TAG_SUCCESS = "success";
private static final String TAG_SCORE = "Score";
private static final String TAG_SEMESTER = "Semester";
private static final String TAG_PRODUCTS = "products";
JSONArray products = null;
ListView list;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.read_result);
    list = (ListView) getListView();
    ResultFetch = new ArrayList<HashMap<String, String>>();
    new LoadResult().execute();
}

class LoadResult extends AsyncTask<String, String, String> {
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ReadResult.this);
        pDialog.setMessage("Loading Result. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        List<NameValuePair> param = new ArrayList<NameValuePair>();

        JSONObject json = jParser.makeHttpRequest(url_readResult, "GET",
                param);


        Log.d("Result: ", json.toString());
        try {
            int success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                JSONObject object = new JSONObject(json.toString());
                products = object.getJSONArray(TAG_PRODUCTS);

                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);
                    String Semester = c.getString(TAG_SEMESTER);
                    String Score = c.getString(TAG_SCORE);
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_SEMESTER, Semester);
                    map.put(TAG_SCORE, Score);
                    ResultFetch.add(map);
                }
            } else {
                 Toast toast = Toast.makeText(getApplicationContext(),
                 "No Result Found", Toast.LENGTH_SHORT);
                 toast.show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String file_url) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {

                ListAdapter adapter = new SimpleAdapter(ReadResult.this,
                        ResultFetch, R.layout.list_item, new String[] {
                                TAG_SEMESTER, TAG_SCORE }, new int[] {
                                R.id.semester, R.id.score });

                list.setAdapter(adapter);

            }
        });

    }

}