以编程方式在 SwipeView 中添加新布局

Posted

技术标签:

【中文标题】以编程方式在 SwipeView 中添加新布局【英文标题】:Programmatically add a new layout inside a SwipeView 【发布时间】:2011-10-02 13:29:52 【问题描述】:

我将尝试以非常清晰易懂的方式解释我的问题。

我目前在这里使用 SwipeView:http://jasonfry.co.uk/?id=23

<uk.co.jasonfry.android.tools.ui.SwipeView  
    android:id="@+id/swipe_view"
    android:layout_ 
    android:layout_>
                <View android:id="@+id/view1" />
                <View android:id="@+id/view2" />
                <View android:id="@+id/view3" />
</uk.co.jasonfry.android.tools.ui.SwipeView>

这很简单,功能强大,当然工作正常。

我现在想要实现的是在这个 SwipeView 中以编程方式添加我的一些布局。

我现在有:

    SwipeView svmain=(SwipeView) findViewById(R.id.svmain);
    //View v= findViewById(R.layout.anylayout);
    svmain.addView(v);

这会崩溃,因为我要添加的 xml 不在我的主布局中。

有什么想法吗?

【问题讨论】:

【参考方案1】:

刚刚找到答案:

    SwipeView svmain=(SwipeView) findViewById(R.id.svmain);
    LayoutInflater inflater = (LayoutInflater)   getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.header, null);
    svmain.addView(view);
    View view2 = inflater.inflate(R.layout.header, null);
    svmain.addView(view2);
    View view3 = inflater.inflate(R.layout.header, null);
    svmain.addView(view3);

【讨论】:

以上是关于以编程方式在 SwipeView 中添加新布局的主要内容,如果未能解决你的问题,请参考以下文章