gpt4 book ai didi

android - 每次单击按钮时如何在特定的 LinearLayout 之后插入一个新的 LinearLayout?

转载 作者:行者123 更新时间:2023-11-30 03:32:48 25 4
gpt4 key购买 nike

我想构建某种 Twitter 应用程序。在 DashboardActivity 中,我需要在每次单击“发布”按钮时添加一个状态框。我的仪表板 xml 如下所示:

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

现在,我希望每次单击“发布”按钮时都能够在水平线布局之后添加一个新的 LinearLayout。我在我的 DashboardActivity 中尝试过这样的事情:

postStatus.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {
addUserStatusBox(firstname,lastname,status);

}});

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 。

您需要创建以下内容:

  1. ListItem 的布局:这表示列表中的单行。您可以通过为此创建单独的布局 来自定义它。假设您创建:listitem_post.xml

  2. 适配器:通过扩展 BaseAdapter 类编写适配器(例如:PostsAdapter.java)。填写所有重写的方法。最重要的是,在 getView() 方法中,膨胀 post_listitem。将其分配给 convertView 对象(作为参数传入)。

    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;
    }
  3. Activity :在您的 Activity xml 代码中,插入一个 ListView,比如 listview_posts。在 Activity 的 java 文件中,在 onCreate() 方法中为 listview_posts 设置在步骤 2 中创建的适配器。

        PostsAdapter postsListAdapter = new PostsAdapter();
    ListView postsListView = (ListView) this.findViewById(R.id.listview_posts);
    postsListView.setAdapter(postsListAdapter);

这就是您指定每个列表元素是 listitem_post 的方式。

Follow this tutorial

关于android - 每次单击按钮时如何在特定的 LinearLayout 之后插入一个新的 LinearLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17163642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com