상대적 레이아웃에 이미지 뷰를 추가하는 데 문제가 있습니다. RelativeLayout을 사용하여 동적으로 만드는 메뉴 항목 목록에 이미지를 추가하고 싶습니다. 내 메뉴 항목은 모두 잘 보이고 순서대로 표시되지만 각 항목에 이미지를 추가하려고하면 화살표가 한 개만 생겨 세로로 가운데가 표시되지 않습니다. 아래는 제 코드입니다. 귀하의 도움을 많이 주시면 감사하겠습니다. 내 코드에서프로그래밍 방식으로 RelativeLayout에서 ImageView를 정렬 할 수 없습니다.
<RelativeLayout
android:id="@+id/pMenu"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
내 XML 파일에서
:
private void buildMenu(String name, int id) {
String[] menuItems = getResources().getStringArray(pMenus[id]);
// Get the rel layout from xml
RelativeLayout container = (RelativeLayout) findViewById(R.id.pMenu);
int imageId=1;
Typeface tf = Typeface.createFromAsset(this.getAssets(),"mreavesmodot-reg.otf");
for(String menuItem: menuItems) {
// Defining the layout parameters
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
StyledButton menuImage = new StyledButton(this);
menuImage.setBackgroundResource(R.drawable.menu_button);
menuImage.setText(menuItem);
menuImage.setTypeface(tf);
menuImage.setTextSize(19);
menuImage.setPadding(20, 0, 0, 0);
menuImage.setTextColor(Color.WHITE);
menuImage.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
menuImage.setOnClickListener(getOnClickListener(menuImage, name));
menuImage.setId(imageId);
if(imageId==1) {
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
} else {
lp.addRule(RelativeLayout.BELOW ,imageId-1);
}
menuImage.setLayoutParams(lp);
ImageView arrow = new ImageView(this);
arrow.setImageResource(R.drawable.arrow_menu);
arrow.setPadding(0, 0, 15, 0);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_RIGHT,menuImage.getId());
params.addRule(RelativeLayout.CENTER_VERTICAL);
arrow.setLayoutParams(params);
container.addView(menuImage);
container.addView(arrow);
imageId++;
}
}
무슨 일이 일어나고 있는지 보여주는 스크린 샷을 추가 할 수 있습니까? 나는 당신이 설명하는 것을 알고 있지만, 이미지는 더 서술적 일 수 있습니다. –