Add view to a vertical LinearLayout at bottom (pro

2019-06-06 01:07发布

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

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.

Adding messages :

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 :

<?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>

1条回答
Lonely孤独者°
2楼-- · 2019-06-06 01:41

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);
查看更多
登录 后发表回答