1

我想建立某種Twitter應用程序。在DashboardActivity我需要添加一個狀態框,每次我點擊「發佈」 button.My儀表板XML看起來是這樣的:如何在每次點擊一個按鈕後在特定的一個之後插入一個新的LinearLayout?

<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); 
} 

這是工作僅在第一次時,我加入status.I不知道如何添加更多的職位後的水平佈局規劃,並能夠看到我的新職位以下的舊職位。我將不勝感激一點幫助。謝謝

+0

我會強烈建議使用一個ListView這個,而不是如果在所有可能的人工建築的每一行。 –

+0

你能給我一個基本的例子,我應該如何做到這一點? – Dianna

回答

1

我會爲此目的使用自定義列表視圖。

您需要創建以下文件:

  1. 佈局列表項:這表示在列表中單列。您可以通過爲此創建單獨的佈局來對其進行定製。說你創建:listitem_post.xml

  2. 適配器:(:PostsAdapter.java說)通過擴展BaseAdapter類寫一個適配器。填寫所有重寫的方法。最重要的是,在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. 活動:在活動的XML代碼,插入一個ListView說listview_posts。在活動的java文件中,爲onview()方法中的listview_posts設置在步驟2中創建的適配器。

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

那你是怎麼指定每個列表元素是listitem_post

Follow this tutorial

+0

謝謝。這很有幫助! – Dianna

+0

很高興我能幫忙:-) – coolscitist

+0

又來了。你有什麼想法如何將所有列表視圖存儲到SharedPreferences?因爲我需要在關閉應用程序並回來後保留所有帖子。 – Dianna

相關問題