Android 将视图添加到底部的垂直线性布局(以编程方式)
我需要以编程方式将视图添加到底部的垂直线性布局中。(是的,我们知道,它在标题中) 好的:我可以向线性布局添加视图,这不是问题。但是它是用来聊天的,所以我需要在底部为显示的每条消息添加视图。我找不到线性布局的任何属性来实现这一点 添加消息:Android 将视图添加到底部的垂直线性布局(以编程方式),android,view,add,android-linearlayout,Android,View,Add,Android Linearlayout,我需要以编程方式将视图添加到底部的垂直线性布局中。(是的,我们知道,它在标题中) 好的:我可以向线性布局添加视图,这不是问题。但是它是用来聊天的,所以我需要在底部为显示的每条消息添加视图。我找不到线性布局的任何属性来实现这一点 添加消息: mHandler.post(new Runnable() { @Override public void run() { TextView message = new TextVi
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>
你可以像这样务实地做,这只是一个例子。您可能希望在此基础上更改代码
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与RelativeLayout一起放在屏幕底部,并在收到新消息时调整其大小。我累了。。。但是谢谢你的帖子,因为它帮助我理解这一点:)