Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在每次单击按钮时在特定线性布局之后插入新的线性布局?_Android_Android Layout_Layout_Android Linearlayout_Android Relativelayout - Fatal编程技术网

Android 如何在每次单击按钮时在特定线性布局之后插入新的线性布局?

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

我想构建某种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>
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);
}

这只是我第一次添加状态时才起作用。我不知道如何在水平规则布局后添加更多帖子,以及如何在我的新帖子下方查看旧帖子。如果能提供一些帮助,我将不胜感激。谢谢你,我将使用自定义列表视图来实现此目的

您需要创建以下内容:

  • 列表项的布局:这表示列表中的单行。您可以为此创建单独的布局来自定义它。假设您创建:listitem\u post.xml

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

  • 活动:在活动的xml代码中插入一个列表视图,比如ListView\u posts。在活动的java文件中,在onCreate()方法中为listview_帖子设置在步骤2中创建的适配器

  • 这就是指定每个列表元素为listitem\u post的方式


    为此,我将使用自定义列表视图

    您需要创建以下内容:

  • 列表项的布局:这表示列表中的单行。您可以为此创建单独的布局来自定义它。假设您创建:listitem\u post.xml

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

  • 活动:在活动的xml代码中插入一个列表视图,比如ListView\u posts。在活动的java文件中,在onCreate()方法中为listview_帖子设置在步骤2中创建的适配器

  • 这就是指定每个列表元素为listitem\u post的方式


    如果可能的话,我强烈建议使用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);