Android:动态创建LinearLayout底部的TextView

时间:2014-06-01 18:58:30

标签: android android-layout scrollview

我创建了一个应用程序,单击按钮我在LinearLayout内部创建了一个TextView(托管在ScrollView中)。 当我单击按钮时,TextViews从顶部开始显示,如下例所示:

第一张图片

http://imgur.com/8fhtYZs(抱歉,我没有声望在帖子中显示,所以我插入了链接)

但我的任务是在LinearLayout底部创建textviews,并使已创建的TextView向上滚动新的。 (抱歉我的英语不好)我举了一个例子,让我更容易理解。

第二张图片

http://imgur.com/RLHooaH

这是我的代码:

public class Messaggi2 extends ActionBarActivity implements OnClickListener {

LinearLayout mLayout;
ScrollView scroll;
Button invia; 

@Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.messaggi2);

    mLayout = (LinearLayout) findViewById(R.id.LinearScrollLayout);
    scroll = (ScrollView) findViewById(R.id.scrollView2);
    invia = (Button) findViewById(R.id.Invia);
    invia.setOnClickListener(this);


}

 @Override
public void onClick(View arg0) {
    mLayout.addView(createNewTextView("Message"));
}

private TextView createNewTextView(String text) {
    final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    final TextView textView = new TextView(this);
    textView.setLayoutParams(lparams);
    textView.setText(text);
    textView.setBackgroundResource(R.drawable.balloon_broadcast_incoming_pressed);
    return textView;
}
}

这是我的Layout.XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="fill_parent"
        android:layout_height="640dp"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin" >

        <LinearLayout
            android:id="@+id/LinearScrollLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/Messaggio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/balloon_broadcast_incoming_pressed"
                android:text="@string/messaggi" />

        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="70dp"
        android:background="@drawable/linear_layout_bg" >

        <EditText
            android:id="@+id/Scrivi"
            android:layout_width="440dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:hint="@string/scriviMessaggio" />

        <Button
            android:id="@+id/Invia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/send_btn" />

    </LinearLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

没有LinearLayout.BOTTOM这样的东西,我们也不能为LinearLayout添加规则。

但好消息是插入到LinearLayout中,可以决定序列..只需使用带有3个参数的addView函数,如下所示

mLayout.addView(view, index, param);

指数确定了订单。

答案 1 :(得分:-2)

您需要将线性布局的高度设置为ScrollView的高度,而不是使用wrap_content因为包裹您的线性布局而将其更改为match_parent

示例:

 <LinearLayout
        android:id="@+id/LinearScrollLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

如果要在linearlayout底部添加按钮,只需在布局参数中添加规则。

示例:

    private TextView createNewTextView(String text) {
    LinearLayout.LayoutParams layoutParams =
                (RelativeLayout.LayoutParams) txt1.getLayoutParams();
    layoutParams.addRule(LinearLayout.BOTTOM, 1);
    final TextView textView = new TextView(this);
    textView.setLayoutParams(lparams);
    textView.setText(text);
    textView.setBackgroundResource(R.drawable.balloon_broadcast_incoming_pressed);
    return textView;
}