Android 如何在每次单击按钮时在特定线性布局之后插入新的线性布局?
我想构建某种twitter应用程序。在DashboardActivity中,每次单击“发布”按钮时,我都需要添加一个状态框。我的仪表板xml如下所示:Android 如何在每次单击按钮时在特定线性布局之后插入新的线性布局?,android,android-layout,layout,android-linearlayout,android-relativelayout,Android,Android Layout,Layout,Android Linearlayout,Android Relativelayout,我想构建某种twitter应用程序。在DashboardActivity中,每次单击“发布”按钮时,我都需要添加一个状态框。我的仪表板xml如下所示: <RelativeLayout> <LinearLayout></LinearLayout> -->header layout <LinearLayout></LinearLayout> -->some layout with some tit
<RelativeLayout>
<LinearLayout></LinearLayout> -->header layout
<LinearLayout></LinearLayout> -->some layout with some titles
<LinearLayout></LinearLayout> --> post status layout with the post button
<LinearLayout></LinearLayout> --> layout with a horizontal rule
<LinearLayout></LinearLayout> --> this is the layout with id "rootStatusBox" where i want to add the status box
</RelativeLayout>
addUserStatusBox()如下所示:
public void addUserStatusBox(String firstname, String lastname,String status) {
LinearLayout rootStatusBox = (LinearLayout)findViewById(R.id.rootStatusBox);
LinearLayout userStatusBox = new LinearLayout(this);
userStatusBox.setOrientation(LinearLayout.HORIZONTAL);
userStatusBox.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setMargins(0, 300, 0, 0); // llp.setMargins(left, top, right, bottom);
userStatusBox.setLayoutParams(layout);
TextView friendName = new TextView(this);
TextView friendStatus = new TextView(this);
TextView dataCrearePost = new TextView(this);
friendName.setText(firstname+ " " + lastname);
friendName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
friendName.setTextSize(10);
friendName.setTypeface(null, Typeface.BOLD);
friendStatus.setText(status);
friendStatus.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(-70, 20, 0, 0); // llp.setMargins(left, top, right, bottom);
friendStatus.setLayoutParams(llp);
friendStatus.setTextSize(10);
userStatusBox.addView(friendName);
userStatusBox.addView(friendStatus);
rootStatusBox.addView(userStatusBox);
}
这只是我第一次添加状态时才起作用。我不知道如何在水平规则布局后添加更多帖子,以及如何在我的新帖子下方查看旧帖子。如果能提供一些帮助,我将不胜感激。谢谢你,我将使用自定义列表视图来实现此目的 您需要创建以下内容:
为此,我将使用自定义列表视图 您需要创建以下内容:
如果可能的话,我强烈建议使用ListView,而不是手动生成每一行。你能给我一个在这种情况下应该如何做的基本示例吗?我强烈建议使用ListView,而不是手动生成每一行。你能给我一个应该如何做的基本示例吗在这种情况下?谢谢。这很有帮助!又来了。您知道如何将所有列表视图存储到SharedReferences中吗?因为在我关闭应用程序并返回后,我需要所有的帖子保持在原位。最简单的方法是保存你的商业模式(帖子列表)。您不需要保存列表项或列表视图,它们应该在加载对象后创建。我想你有一个“Post”之类的课程。使该类实现可序列化接口。如果您具有该类的属性,这些属性是应该保存和检索的对象,则将它们也序列化//写入:FileOutputStream fos=new FileOutputStream(context.getFilesDir()+File.separator+filename);ObjectOutputStream oos=新的ObjectOutputStream(fos);oos.writeObject(posts);//要读取:FileInputStream fis=新的FileInputStream(context.getFilesDir()+File.separator+filename);ObjectInputStream ois=新ObjectInputStream(fis);posts=()ois.readObject();谢谢。这很有帮助!又来了。您知道如何将所有列表视图存储到SharedReferences中吗?因为在我关闭应用程序并返回后,我需要所有的帖子保持在原位。最简单的方法是保存你的商业模式(帖子列表)。您不需要保存列表项或列表视图,它们应该在加载对象后创建。我想你有一个“Post”之类的课程。使该类实现可序列化接口。如果您具有该类的属性,这些属性是应该保存和检索的对象,则将它们也序列化//写入:FileOutputStream fos=new FileOutputStream(context.getFilesDir()+File.separator+filename);ObjectOutputStream oos=新的ObjectOutputStream(fos);oos.writeObject(posts);//要读取:FileInputStream fis=新的FileInputStream(context.getFilesDir()+File.separator+filename);ObjectInputStream ois=新ObjectInputStream(fis);posts=()ois.readObject();
public void addUserStatusBox(String firstname, String lastname,String status) {
LinearLayout rootStatusBox = (LinearLayout)findViewById(R.id.rootStatusBox);
LinearLayout userStatusBox = new LinearLayout(this);
userStatusBox.setOrientation(LinearLayout.HORIZONTAL);
userStatusBox.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layout = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setMargins(0, 300, 0, 0); // llp.setMargins(left, top, right, bottom);
userStatusBox.setLayoutParams(layout);
TextView friendName = new TextView(this);
TextView friendStatus = new TextView(this);
TextView dataCrearePost = new TextView(this);
friendName.setText(firstname+ " " + lastname);
friendName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
friendName.setTextSize(10);
friendName.setTypeface(null, Typeface.BOLD);
friendStatus.setText(status);
friendStatus.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(-70, 20, 0, 0); // llp.setMargins(left, top, right, bottom);
friendStatus.setLayoutParams(llp);
friendStatus.setTextSize(10);
userStatusBox.addView(friendName);
userStatusBox.addView(friendStatus);
rootStatusBox.addView(userStatusBox);
}
public View getView(int index, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(R.layout.listitem_post, parent, false);
}
//Code other parts
return convertView;
}
PostsAdapter postsListAdapter = new PostsAdapter();
ListView postsListView = (ListView) this.findViewById(R.id.listview_posts);
postsListView.setAdapter(postsListAdapter);