2013-06-03 63 views
1

我有一個LinearLayout。我在運行時添加項目。但所有項目都顯示從TOP到BOTTOM。現在我正在嘗試顯示BOTTOM到TOP的項目。LinearLayout顯示項目底部向上

我是說。我想從BOTTOM開始,以設置線性佈局中的項目。

這是我的線性佈局: -

messagesContainer = (ViewGroup) findViewById(R.id.messagesContainer); 
    scrollContainer = (ScrollView) findViewById(R.id.scrollContainer); 

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


    if (!leftSide) { 
     bgRes = R.drawable.right_message_bg; 
     params.gravity = Gravity.RIGHT; 
     params.leftMargin=30; 
    } 
    else 
    { 
     params.gravity = Gravity.LEFT; 
     params.rightMargin=30; 
    } 

    textView.setLayoutParams(params); 

    textView.setBackgroundResource(bgRes); 

    runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      messagesContainer.addView(textView,messagesContainer.getChildCount()); 

      //messagesContainer.bringChildToFront(textView); 

      // Scroll to bottom 
      if (scrollContainer.getChildAt(0) != null) { 
       scrollContainer.scrollTo(scrollContainer.getScrollX(), scrollContainer.getChildAt(0).getHeight()); 
      } 
      scrollContainer.fullScroll(View.FOCUS_DOWN); 
      scrollContainer.pageScroll(View.FOCUS_DOWN); 
     } 
    }); 

請幫助我。

回答

4

您可以用編程方式添加:

LinearLayout layout = findViewById(R.id.layout); 
layout.addView(newView, index); 

你只需要具有指數= 0

隨時補充而且你還可以使用android:gravity="bottom"

+0

我在 「的ViewGroup」 添加。我更新了我的代碼。 – user2160008

+0

我什麼都不工作。它只是在視圖組中顯示消息。但msg從上到下顯示。我想顯示它底部到頂部 – user2160008

+0

我想給底部文本索引= 0 – user2160008

1

重力。

另外,如果你想將消息從底部顯示到頂部,你需要將它們添加 到ViewGroup的順序。

設置引力的BOTTOM

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT); 
      params.weight = 1.0f; 
      params.gravity=48; 

      button.setLayoutParams(params); 

重力值,以及如何設置重力檢查Gravity