【问题标题】:LinearLayout display item bottom to upLinearLayout 从下到上显示项目
【发布时间】:2013-06-03 21:30:43
【问题描述】:

我有一个线性布局。我正在运行时添加项目。但所有项目都是从顶部到底部显示的。现在我正在尝试从 BOTTOM 到 TOP 显示项目。

我的意思是。我想从 BOTTOM 到 TOP 以线性布局设置项目。

这是我的线性布局:-

 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);
        }
    });

请帮助我。

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:

    您可以通过以下方式以编程方式添加它:

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

    您只需始终添加 index = 0

    你也可以使用android:gravity="bottom"

    【讨论】:

    • 我很抱歉无法正常工作。它只是视图组中的消息显示。但味精是从上到下显示的。我想从下到上显示它
    • 我想将底部文本提供给 index = 0
    • 你给了 layout.setGravity(Gravity.BOTTOM);
    • 但我的底部文本索引是总味精。
    • 如果您与线性混淆,请使用相对
    【解决方案2】:

    重力。

    另外,如果你想让消息从下到上显示,你需要添加 他们按顺序发送到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

    【讨论】:

      猜你喜欢
      • 2012-02-27
      • 2017-10-26
      • 2012-06-17
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 1970-01-01
      相关资源
      最近更新 更多