2013-05-16 31 views
0

我想以編程方式對齊RelativeLayout中的一些元素,但我遇到了一些問題。我所有的TextView元素都與左上角對齊,即使我設置它們的時候也是如此。這裏是我的屏幕截圖: enter image description hereRelativeLayout參數以編程方式對齊錯誤

這裏是我的代碼(this是RelativeLayout的):

 title = new TextView(context); 
     date = new TextView(context); 
     rating = new RatingBar(context); 
     saleImage = new ImageView(context); 
     arrowImage = new ImageView(context); 

     RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     this.setBackgroundResource(R.drawable.list_selector); 
     this.setLayoutParams(relativeLayoutParams); 
     this.setClickable(true); 

     relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     saleImage.setLayoutParams(relativeLayoutParams); 
     saleImage.setImageResource(R.drawable.rihanna); 
     this.addView(saleImage); 

     relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE); 
     relativeLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); 
     arrowImage.setImageResource(R.drawable.arrow); 
     arrowImage.setLayoutParams(relativeLayoutParams); 
     this.addView(arrowImage); 

     relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
     relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, saleImage.getId()); 
     title.setLayoutParams(relativeLayoutParams); 
     title.setTextAppearance(context, android.R.style.TextAppearance_Medium); 
     title.setText("Sale title"); 
     this.addView(title); 

     relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     relativeLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE); 
     relativeLayoutParams.setMargins(0, 0, 26, 0); 
     relativeLayoutParams.addRule(RelativeLayout.LEFT_OF, arrowImage.getId()); 
     date.setTextAppearance(context, android.R.style.TextAppearance_Small); 
     date.setText("14.01.13 22:00"); 
     this.addView(date); 

看來這個問題是與RelativeLayout.RIGHT_OFRelativeLayout.LEFT_OF屬性。

這裏是活動代碼:

public class MainPage extends Activity { 
    private LinearLayout test; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_sales); 

     test = (LinearLayout)findViewById(R.id.salesConteiner); 
     SaleRow row = new SaleRow(this); 
     test.addView(row); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main_page, menu); 
     return true; 
    } 

} 

有什麼我錯過了什麼?謝謝!!!

+0

看看[這](http://stackoverflow.com/questions/10185368/android-dynamic-relativelayout-overlaps-each-other)可以幫助你。 – Neoh

+0

嘗試爲每個視圖創建一個新的'RelativeLayout.LayoutParams'對象,而不是重複使用相同的'relativeLayoutParams'對象。 –

+0

@John Leehey它沒有幫助 – vlio20

回答

0

對於view.getId()函數的工作,你首先需要一個ID:view.setId(1)

感謝您的幫助!

相關問題