2012-12-05 86 views
0

我有一個圖像放置在相對佈局。我如何在背景圖像上放置一個小圖像,並在圖像旁邊放置兩個文本視圖。任何幫助將非常感激。唯一的事情是我想以編程方式做到這一點。我嘗試了以下方法,但出於某種原因,文本視圖出現在屏幕的左上角。我希望它出現在imagebutton上。如何以編程方式覆蓋圖像上的文本

LinearLayout LinearLayout = new LinearLayout(this); 
RelativeLayout relativeLayout = new RelativeLayout(this); 
RelativeLayout.LayoutParams relativeLayoutParams; 

Button[] btn = new Button[1]; 
btn[0] = new Button(this); 
btn[0].setId(1); 
btn[0].setGravity(Gravity.CENTER); 
btn[0].setText("text");   
btn[0].setBackgroundResource(R.drawable.button_grey); 
btn[0].setTextColor(Color.parseColor("#FFFFFF")); 


relativeLayoutParams = new  RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                 RelativeLayout.LayoutParams.WRAP_CONTENT); 

relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT | RelativeLayout.ALIGN_PARENT_TOP); 

relativeLayoutParams.setMargins(20, 20, 15, 15); 

relativeLayout.addView(btn[0], relativeLayoutParams); 

ImageButton[] Imgbtn = new ImageButton[10]; 
Imgbtn[0] = new ImageButton(this); 
Imgbtn[0].setId(2); 
Imgbtn[0].setBackgroundResource(R.drawable.box_round_corners); 
Imgbtn[0].setMaxHeight(200); 
Imgbtn[0].setClickable(true); 

relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                 RelativeLayout.LayoutParams.WRAP_CONTENT); 


relativeLayoutParams.addRule(RelativeLayout.BELOW,btn[0].getId()); 
relativeLayoutParams.height = 100;  
relativeLayoutParams.setMargins(15, 0, 15, 10); 
Imgbtn[0].setLayoutParams(relativeLayoutParams); 

relativeLayout.addView(Imgbtn[0], relativeLayoutParams); 

TextView[] txtview = new TextView[10]; 
txtview[0] = new TextView(this); 
txtview[0].setId(3); 
txtview[0].setTextColor(Color.parseColor("#000000")); 
txtview[0].setText("text"); 

relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                 RelativeLayout.LayoutParams.WRAP_CONTENT); 

// relativeLayoutParams.addRule(RelativeLayout.ALIGN_LEFT | RelativeLayout.ALIGN_TOP);

relativeLayoutParams.addRule(RelativeLayout.BELOW,Imgbtn[0].getId()); 
relativeLayoutParams.height = 20;  
relativeLayoutParams.setMargins(5, 5, 5, 5); 
txtview[0].setLayoutParams(relativeLayoutParams); 

relativeLayout.addView(txtview[0], relativeLayoutParams); 


android.widget.ScrollView ScrollV = new ScrollView(this); 
ScrollV.addView(relativeLayout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
addContentView(ScrollV, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
+0

*我想以編程的方式做到這一點* =>是的,但首先[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

+0

@ PareshMayani我編輯了我的問題請看看 –

回答

0

嘗試使用setLayoutParams()方法設置您的視圖LayoutParams;

RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(width,height); 
lp.topMargin=//what is your desired y coordinate 
lp.leftMargin=//what is your desired x coordinate 
lp.gravity=Gravity.NO_GRAVITY; 
view.setLayoutParams(lp); 

在我看來,FrameLayout更適合覆蓋目的。

+1

我同意。這正是'FrameLayout'的用例。 –