RadioGroup自動新增RadioButton實現螢幕適配
阿新 • • 發佈:2019-02-11
mRadioGroup = (RadioGroup) findViewById(R.id.main_radio_group); View views; RadioButton radioButton; if (i == 0) { views = getLayoutInflater().inflate(R.layout.layout_table_group_radiobutton, null); radioButton = (RadioButton) views.findViewById(R.id.main_radio_nail); } else if (i>0 && i<data.length()-1) { views = getLayoutInflater().inflate(R.layout.layout_table_group_center_radiobutton, null); radioButton = (RadioButton) views.findViewById(R.id.center_radio_nail); }else { views = getLayoutInflater().inflate(R.layout.layout_table_group_right_radiobutton, null); radioButton = (RadioButton) views.findViewById(R.id.right_radio_nail); }
//螢幕適配,用螢幕解析度來設定radiobutton的寬和高 DisplayMetrics metric = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metric); int width = (int) (metric.widthPixels/4.5); int height = metric.heightPixels/15; radioButton.setWidth(width); radioButton.setHeight(height);
radioButton.setText(projectName + "");
radioButton.setId(i + 1);
mRadioGroup.addView(radioButton);