2014-03-04 96 views
2

我有一個用ImageView和TextView的RelativeLayout。我想要在ImageView下面的TextView(帶有一個小填充),並且ImageView和TextView都對齊到RelativeLayout的中心。RelativeLayout與ImageView和TextView - 對齊下面的ImageView的TextView,兩者都以編程方式在RelativeLayout中心

的RelativeLayout的是一個編程

加我已閱讀SO關於對準了一些問題,但是,我不能讓他們爲我工作。下面是我當前的代碼...

代碼的RelativeLayout

RelativeLayout relativeLayout = new RelativeLayout(this); 

代碼ImageView的

 ImageView image = new ImageView(this); 
     RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 

     lpImage.addRule(RelativeLayout.CENTER_IN_PARENT); 

     //Setting the parameters on the Image 
     image.setLayoutParams(lpImage); 

     //adding imageview to relative layout 
     relativeLayout.addView(image); 

代碼的TextView

TextView textview = new TextView(this); 
RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT); 
      lpTextView.addRule(RelativeLayout.BELOW, image.getId()); 

      //Setting the parameters on the TextView 
      textview.setLayoutParams(lpTextView); 

      //Adding TextView to relative layout 
      relativeLayout.addView(textview); 

如果我設置RelativeLayout.CENTER_IN_PARENT兩個圖像和文本,它們相互重疊,這是可以理解的,因爲RelativeLayout支持overlapp意見。

我認爲爲textview設置RelativeLayout.BELOW會使它自己在圖像下面對齊,但事實並非如此。我甚至試過RelativeLayout.ALIGN_BOTTOM爲textview,但即使這不起作用。

回答

5

試試這個..

RelativeLayout relativeLayout = new RelativeLayout(this); 

RelativeLayout.LayoutParams lprela = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.MATCH_PARENT, 
      RelativeLayout.LayoutParams.MATCH_PARENT); 
relativeLayout.setLayoutParams(lprela); 

ImageView image = new ImageView(this); 
    RelativeLayout.LayoutParams lpImage = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 

    lpImage.addRule(RelativeLayout.ALIGN_PARENT_TOP); 

    //Setting the parameters on the Image 
    image.setLayoutParams(lpImage); 

    //adding imageview to relative layout 
    relativeLayout.addView(image); 

    TextView textview = new TextView(this); 
    RelativeLayout.LayoutParams lpTextView = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 
     lpTextView.addRule(RelativeLayout.BELOW, image.getId()); 

     //Setting the parameters on the TextView 
     textview.setLayoutParams(lpTextView); 

     //Adding TextView to relative layout 
     relativeLayout.addView(textview); 

或者

LinearLayout linearLayout = new LinearLayout(this); 
LinearLayout.LayoutParams lp_ineer_ver = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT, 
       LinearLayout.LayoutParams.MATCH_PARENT); 
linearLayout.setGravity(Gravity.CENTER); 
linearLayout.setOrientation(LinearLayout.VERTICAL); 
linearLayout.setLayoutParams(lp_ineer_ver); 

ImageView image = new ImageView(this); 
     LinearLayout.LayoutParams lpImage = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT); 

     //Setting the parameters on the Image 
     image.setLayoutParams(lpImage); 

     //adding imageview to relative layout 
     linearLayout.addView(image); 

TextView textview = new TextView(this); 
LinearLayout.LayoutParams lpTextView = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT); 

      //Setting the parameters on the TextView 
      textview.setLayoutParams(lpTextView); 

      //Adding TextView to relative layout 
      linearLayout.addView(textview); 
+0

我將測試這兩個佈局,並會回覆給你.. –

+0

我得到它使用LinearLayout方法的工作。感謝你的回答。但是,有一個小的改變..而不是在layoutparams中使用Gravity.CENTER,使用linearLayout.setGravity(Gravity.CENTER)爲我工作。請相應地更改您的答案。謝謝..答覆接受。 –

+0

@VamsiChalla感謝編輯的信息。 – Hariharan

0

下面過程也可能爲你工作。 1)添加圖像視圖&垂直LinearLayout中的文本視圖。 2)將線性佈局添加到相對佈局中心(使用CENTER_IN_PARENT)。

相關問題