将视图添加到底部的垂直LinearLayout(以编程方式) [英] Add view to a vertical LinearLayout at bottom (programmatically)

查看:96
本文介绍了将视图添加到底部的垂直LinearLayout(以编程方式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式在底部的垂直Linearlayout中添加视图。 (是的,我们知道,它在标题中。)

I need to add views to a vertical Linearlayout at bottom programmatically. (Yes we know that, it's in the title).

好:我可以在线性布局中添加视图,这不是问题。但这是聊天,因此我需要在显示的每条消息的底部添加视图。我没有找到LinearLayout的任何属性来执行此操作。

Ok : i can add views to the linearlayout, it's not a problem. But it's for a chat, so i need to add views at the bottom for each message which is displayed. I don't find any attributes to the LinearLayout to do that.

添加消息:

mHandler.post(new Runnable() {
            @Override
            public void run() {
                TextView message = new TextView(ChatActivity.this);
                String[] splitMessage = text.split(":");
                message.setText(splitMessage[0]+"\n"+splitMessage[1]);
                message.setGravity(Gravity.LEFT);
                message.setMaxWidth(200);
                message.setBackgroundColor(getResources().getColor(android.R.color.tertiary_text_light));
                message.setTextColor(getResources().getColor(android.R.color.black));
                message.setSingleLine(false);
                mLayout.addView(message);
            }
        });

chat_layout.xml:

chat_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/live_obs_mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/empty"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/chat_layout"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="10" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:isScrollContainer="tue"
             >
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="bottom"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/edit_chat"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:layout_gravity="left|center_vertical"
            android:layout_weight="5" />

        <Button
            android:id="@+id/chat_submit"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:layout_gravity="right|center_vertical"
            android:onClick="onSubmit"
            android:text="OK" />
    </LinearLayout>

</LinearLayout>


推荐答案

您可以这样实际地进行操作,这只是一个例子。您可能要基于此更改代码。

You can do it pragmatically like this, this is just an example. You may want to change your code based on that.

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
    TextView txt1 = new TextView(MyClass.this);
    LinearLayout.LayoutParams layoutParams =
                (RelativeLayout.LayoutParams) txt1.getLayoutParams();
    layoutParams.addRule(LinearLayout.BOTTOM, 1);
    txt1.setLayoutParams(layoutParams);
    linearLayout.addView(txt1);

这篇关于将视图添加到底部的垂直LinearLayout(以编程方式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆