我创建了一个应用程序,点击一个按钮,我创建一个TextView内的LinearLayout(托管在ScrollView)。当我点击按钮时,TextViews从顶部开始显示,就像下面的例子:
第一图像
https://i.stack.imgur.com/s4SeA.jpg(对不起,我没有信誉,所以我插入了链接)
但是我的任务是在我的LinearLayout底部创建textviews,并使已经创建的textviews向上滚动到新的textviews。(对不起,我的英语不好)我举一个例子,让你更容易理解。
<<p> 第二图像/strong> https://i.stack.imgur.com/5NcPF.jpg我的代码:
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>
没有LinearLayout这种东西。也不能为LinearLayout添加规则
但好消息是插入到LinearLayout中,可以决定顺序。只需使用带有3个参数的addView函数,如下所示
mLayout.addView(view, index, param);
索引决定了顺序
你需要将你的线性布局的高度设置为你的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;
}