【问题标题】:Android : can't add view programmatically in linearLayout androidAndroid:无法在linearLayout android中以编程方式添加视图
【发布时间】:2015-04-25 14:19:36
【问题描述】:

我有一个片段,它具有滚动视图作为布局的父级,并且在滚动视图中我有一个 LinearLayout 。当我尝试以编程方式将视图添加到该 LinearLayout 但我收到错误“指定的孩子已经有一个父母。您必须首先在孩子的父母上调用 removeView()。”

这是我的布局

<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, container, false); => container?不是newsListViewnewsListView.addView(newsListView);
  • newsListView.addView(newsListView); ??!!
  • @Selvin 我需要一个类似列表视图的东西,但它不应该是可滚动的
  • 在添加视图之前,调用newsListView.removeView(view);删除它
  • @MortezaSoleimani 哦!这是造成问题的原因。 !感谢您的帮助

标签: android view android-linearlayout android-scrollview


【解决方案1】:

在代码 sn-p newsListView.addView(newsListView) 中,您正在为其自身添加一个视图。我想你的意思是:

newsListView.addView(newsListRow);

...回到上下文中...

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

【讨论】:

    【解决方案2】:

    很明显你得到了那个错误。看你正在膨胀一个视图,当你的循环运行时,它第一次在你的线性布局中添加了那个视图。当您的循环下次运行时,它会采用您之前添加的该视图的相同对象。由于这是一个静态视图,因此在初始化时不会创建新对象。

    你应该使用动态视图。

    以下就是你的答案

        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 使其看起来像您的静态行视图。

    就是这样......享受...... :-)

    【讨论】:

    • 实际上在循环中膨胀相同的 xml 每次都会创建一个新对象。不必在代码中而不是在 xml 中进行。
    【解决方案3】:

    在添加视图之前,调用删除它

    newsListView.removeView(view);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 2013-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多