我有一個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;
}
刪除'inflater.inflate(R.layout.list_news_row0,容器,FALSE) ;'=>'容器'?而不是'newsListView'? 'newsListView.addView(newsListView);'<=認真你wana添加視圖到自己?也使用ListView代替 – Selvin 2015-02-24 03:33:41
newsListView.addView(newsListView); ?? !! – 2015-02-24 03:36:59
@Selvin我需要一個像listview的東西,但不應該滾動 – user2549089 2015-02-24 03:37:14