2012-10-31 112 views
0

我有一個創建在垂直的LinearLayout 3個textViews的方法:位置視圖編程

public LinearLayout CreateLayout() { 

    LinearLayout aLap = new LinearLayout(this); 

    LinearLayout.LayoutParams TextParams = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, 
      LinearLayout.LayoutParams.WRAP_CONTENT); 

    TextView txtName = new TextView(this); 
    txtName.setText(result1); 

    txtName.setLayoutParams(TextParams); 
    txtName.setGravity(Gravity.CENTER); 
    txtName.setPadding(20, 8, 20, 4); 
    txtName.setTextSize(20); 
    txtName.setTextColor(Color.parseColor("#000000")); 

    TextView txtTime = new TextView(this); 
    txtTime.setText(result2); 
    txtTime.setLayoutParams(TextParams); 
    txtTime.setGravity(Gravity.CENTER); 
    txtTime.setTextSize(20); 
    txtTime.setPadding(20, 4, 10, 4); 
    txtTime.setTextColor(Color.parseColor("#000000")); 

    android.view.ViewGroup.LayoutParams Params = new ViewGroup.LayoutParams(
      android.view.ViewGroup.LayoutParams.MATCH_PARENT, 150); 

    TextView rep = new TextView(this); 
    String Rep = sharedPrefs.getString("R_PREFS", "2"); 

    rep.setLayoutParams(TextParams); 
    rep.setGravity(Gravity.CENTER); 
    rep.setTextSize(20); 
    rep.setPadding(20, 4, 10, 4); 
    rep.setTextColor(Color.parseColor("#000000")); 
    rep.setText("Test: " + Rep); 

    aLap.setLayoutParams(Params); 
    aLap.setBackgroundResource(R.drawable.bg); 
    aLap.setOrientation(LinearLayout.VERTICAL); 
    aLap.setPadding(3, 3, 3, 3); 
    aLap.addView(txtName); 
    aLap.addView(txtTime); 
    aLap.addView(rep); 

    return aLap; 

} 

我使用垂直佈局,因爲我希望textViews被置於彼此。現在我想要一個ImageView放置在這個textViews的右側並垂直居中。那可能嗎?

回答

1

不能達到預期的輸出與LinearLayout。我建議你使用RelativeLayout。它更靈活。通過向視圖添加規則,您可以在期望的位置添加更多子視圖到RelativeLayout。 檢查這篇文章,瞭解如何在運行時使用Relative Layout時添加規則:How to lay out Views in RelativeLayout programmatically?

0
<LinearLayout orientation="horizontal"> 
    <LinearLayout orientation="vertical"> 
    <TextView view1/> 
    <TextView view1/> 
    <TextView view1/> 
    </LinearLayout> 
    <ImageView height=fill_parent scaleType="center"/> 
</LinearLayout> 

或多或少你想要什麼,我認爲