2015-02-24 98 views
0

我有一個scrollview作爲佈局的父級,而在scrollview中我有一個LinearLayout。當我試圖以編程方式將視圖添加到該LinearLayout但我得到的錯誤「指定的孩子已經有一個父母,你必須先調用孩子的父母removeView()。」Android:不能在linearLayout中以編程方式添加視圖android

這是我的佈局

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layoutDirection="ltr" 
tools:context="ir.pnuopen5.application.fragment.LeaugeFragment"> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin"> 

    <TextView 
     android:id="@+id/tv_LeagueFragment_title_lastNews" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/last_news" 
     android:textSize="17sp" 
     android:layout_gravity="right" 
     android:layout_alignParentTop="true" 
     android:layout_alignRight="@+id/view1" 
     android:layout_alignEnd="@+id/view1" /> 

    <View 
     android:id="@+id/view1" 
     android:layout_width="match_parent" 
     android:layout_height="3dp" 
     android:layout_marginTop="10dp" 
     android:background="@color/line" 
     android:layout_below="@+id/tv_LeagueFragment_title_lastNews" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/lv_LeagueFragment_lastNews" 
     android:layout_below="@+id/view1" 
     android:orientation="horizontal"/> 

    ... 

,這是我的java代碼

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_leauge, container, false); 

    ... 

    LinearLayout newsListView = (LinearLayout)view.findViewById(R.id.lv_LeagueFragment_lastNews); 
    View newsListRow; 
    TextView title,time; 


    for (int i = 0;i<newsList.size();i++){ 
     newsListRow = inflater.inflate(R.layout.list_news_row0, container, false); 
     title = (TextView)newsListRow.findViewById(R.id.rowNewsTitle); 
     time = (TextView)newsListRow.findViewById(R.id.rowNewsTime); 
     title.setText(newsList.get(i).getTitle()); 
     time.setText(newsList.get(i).getTime()); 
     newsListView.addView(newsListView); 
    } 

    ... 

    return view; 
} 
+1

刪除'inflater.inflate(R.layout.list_news_row0,容器,FALSE) ;'=>'容器'?而不是'newsListView'? 'newsListView.addView(newsListView);'<=認真你wana添加視圖到自己?也使用ListView代替 – Selvin 2015-02-24 03:33:41

+0

newsListView.addView(newsListView); ?? !! – 2015-02-24 03:36:59

+0

@Selvin我需要一個像listview的東西,但不應該滾動 – user2549089 2015-02-24 03:37:14

回答

1

中的代碼片斷newsListView.addView(newsListView),要添加一個視圖本身。我想你的意思做的是:

​​

...把​​它放回背景...

for (int i = 0;i<newsList.size();i++){ 
    newsListRow = inflater.inflate(R.layout.list_news_row0, container, false); 
    title = (TextView)newsListRow.findViewById(R.id.rowNewsTitle); 
    time = (TextView)newsListRow.findViewById(R.id.rowNewsTime); 
    title.setText(newsList.get(i).getTitle()); 
    time.setText(newsList.get(i).getTime()); 
    newsListView.addView(newsListRow); 
} 
0

,你得到這個錯誤很明顯。看起來你正在膨脹一個視圖,第一次當你的循環運行時,它會在你的線性佈局中添加該視圖。當下一次循環運行時,它將採用與之前添加的視圖相同的對象。由於這是一個靜態視圖,因此它在初始化時不會創建一個新對象。

您應該使用動態視圖。

以下是你的答案

for(int i = 0;i<newsList.size();i++) { 
     LinearLayout newsListRow = new LinearLayout(this); 
     TextView title = new TextView(this); 
     TextView time = new TextView(this); 

     title.setText(newsList.get(i).getTitle()); 
     time.setText(newsList.get(i).getTime()); 

     newsListRow.addView(title); 
     newsListRow.addView(time); 

     newsListView.addView(newsListView); 
    } 

您可以添加到LayoutParameter使它看起來像你的靜態行視圖。

這是它... ...享受:-)

+0

實際上膨脹相同的xml在循環中每次都會創建一個新對象。沒有必要用代碼而不是xml來完成。 – 2015-02-24 04:38:03

0

之前添加視圖,通過調用

newsListView.removeView(view); 
相關問題