में ImageView को संरेखित नहीं कर सकता है मुझे किसी सापेक्ष लेआउट में एक छविदृश्य जोड़ने में समस्या हो रही है। मैं मेन्यू आइटम्स की एक सूची में एक छवि जोड़ना चाहता हूं जिसे मैं रिलेवेटिवआउट का उपयोग करके गतिशील रूप से बना रहा हूं। मेरे सभी मेनू आइटम बस ठीक और क्रम में दिखाए जाते हैं, लेकिन जब मैं प्रत्येक आइटम में एक छवि जोड़ने की कोशिश करता हूं तो मुझे केवल एक तीर मिलता है और यह लंबवत केंद्रित नहीं होता है। नीचे मेरा कोड है। आपका सहयोग सराहनीय है।प्रोग्रामेटिक रूप से RelativeLayout
मेरी एक्सएमएल फ़ाइल
<RelativeLayout
android:id="@+id/pMenu"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
मेरी कोड में:
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++;
}
}
क्या आप एक स्क्रीनशॉट जोड़ सकते हैं जो दिखा रहा है कि क्या हो रहा है? मुझे पता है कि आप समझाते हैं, लेकिन एक छवि अधिक वर्णनात्मक हो सकती है। –