나는 안드로이드 개발이 처음인데, 내 프로젝트에서 상대 레이아웃을 사용하여 체크 박스를 수평으로 정렬하고 싶습니다. 선형 레이아웃을 사용하면 더 쉽습니다.
제 경우에는 같은 위치에 붙여 넣은 체크 박스
Main = (RelativeLayout) dialogLayout.findViewById(R.id.linearr);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinkedHashMap<String, String> alphabet = new LinkedHashMap<String, String>();
alphabet.put("1", "A");
alphabet.put("2", "B");
alphabet.put("3", "C");
alphabet.put("4", "D");
alphabet.put("5", "A");
alphabet.put("6", "B");
alphabet.put("7", "C");
alphabet.put("8", "D");
Set<?> set = alphabet.entrySet(); // Get an iterator
Iterator<?> i = set.iterator(); // Display elements
while (i.hasNext()) {
@SuppressWarnings("rawtypes")
Map.Entry me = (Map.Entry) i.next();
checkBox = new CheckBox(getContext());
checkBox.setId(Integer.parseInt(me.getKey().toString()));
checkBox.setText(me.getValue().toString());
params.rightMargin = 30;
if(Integer.parseInt(me.getKey().toString())==1)
params.addRule(RelativeLayout.ALIGN_LEFT);
else
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,Integer.parseInt(me.getKey().toString())-1);
checkBox.setLayoutParams(params);
Main.addView(checkBox, params);
`도움을 주시면 감사하겠습니다.
당신은 그것을 사용하여 할 수 있습니다
List<String> name = new ArrayList<>();
List<Integer> id = new ArrayList<>();
for (int i = 0; i < 8; i++) {
name.add("C" + (i + 1));
id.add(generateViewId());
}
for (int i = 0; i < name.size(); i++) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
CheckBox checkBox = new CheckBox(this);
checkBox.setId(id.get(i));
checkBox.setText(name.get(i));
if (i != 0) {
params.addRule(RelativeLayout.RIGHT_OF, id.get(i-1));
}
checkBox.setLayoutParams(params);
container.addView(checkBox, params);
}
- 그들은 고유하도록 ID를 생성하려면 고유 ID를 생성합니다 .
/**
* params
* Generate a value suitable for use in
* This value will not collide with ID values generated at build time by aapt for R.id.
*
* @return a generated ID value
*/
public static int generateViewId() {
for (; ; ) {
final int result = sNextGeneratedId.get();
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
int newValue = result + 1;
if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
if (sNextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
}
출력 :
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다