2012-05-18 59 views
1

是否可以将视图添加到与OnCreate()中的setContentView()中调用的不同布局?在Android中以编程方式添加视图

我试图使用Zylincs ViewPager(在http://www.zylinc.com/blog-reader/items/viewpager-page-indicator.html找到),我有那部分设置和工作很好。

这使我留下的是4个布局。 1是main.xml,其他三个是main_pg0.xml,main_pg1.xml和main_pg3.xml

这三个页面是每个“页面”的内容是main.xml包含页面查看器的位置。

在使用setContentView(main.xml)的onCreate im中。

即时尝试现在要做的是能够将textViews编程地添加到一些页面。

我有现在的问题是:

LayoutInflater factory = getLayoutInflater(); 

View layout = factory.inflate(R.layout.main_pg0, null); 
View linearLayout = layout.findViewById(R.id.linearLayout); 

TextView valueTV = new TextView(this); 
valueTV.setText("hallo hallo"); 
valueTV.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 

((LinearLayout) linearLayout).addView(valueTV); 

这是据我已经能够得到的,不添加TextView的都没有。

---- ----编辑

希望能澄清多一点什么我尝试做 继承人我的代码

Main.java

public class Main extends FragmentActivity implements PageInfoProvider { 
    public GlobalVars globalVars; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    globalVars = (GlobalVars) getApplicationContext(); 

    MyPagerAdapter adapter = new MyPagerAdapter(); 
    ViewPager myPager = (ViewPager) findViewById(R.id.viewpager); 
    myPager.setAdapter(adapter); 
    myPager.setCurrentItem(0); 

    ViewPagerIndicator indicator = (ViewPagerIndicator) findViewById(R.id.indicator); 
    myPager.setOnPageChangeListener(indicator); 

    indicator.init(0, adapter.getCount(), this); 

    Resources res = getResources(); 
    Drawable prev = res.getDrawable(R.drawable.indicator_prev_arrow); 
    Drawable next = res.getDrawable(R.drawable.indicator_next_arrow); 

    indicator.setArrows(prev, next); 
    } 

    private class MyPagerAdapter extends PagerAdapter { 

    public int getCount() { 
     return 3; 
    } 

    public Object instantiateItem(View collection, int position) { 

     LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     int resId = 0; 
     switch (position) { 
     case 0: 
     resId = R.layout.main_pg0; 
     LoadVehicles(); 
     break; 
     case 1: 
     resId = R.layout.main_pg1; 
     break; 
     case 2: 
     resId = R.layout.main_pg2; 
     break; 
     } 

     View view = inflater.inflate(resId, null); 
     ((ViewPager) collection).addView(view, 0); 

     return view; 
    } 

    @Override 
    public void destroyItem(View arg0, int arg1, Object arg2) { 
     ((ViewPager) arg0).removeView((View) arg2); 

    } 

    @Override 
    public void finishUpdate(View arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public boolean isViewFromObject(View arg0, Object arg1) { 
     return arg0 == ((View) arg1); 
    } 

    @Override 
    public void restoreState(Parcelable arg0, ClassLoader arg1) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public Parcelable saveState() { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void startUpdate(View arg0) { 
     // TODO Auto-generated method stub 

    } 

    } 

    public String getTitle(int position) { 
    String title = "--Untitled--"; 
    switch (position) { 
    case 0: 
     title = "Page 0"; 
     break; 
    case 1: 
     title = "Page 1"; 
     break; 
    case 2: 
     title = "Page 2"; 
     break; 
    } 
    return title; 
    } 
} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <com.zylinc.view.ViewPagerIndicator 
    android:id="@+id/indicator" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#EAEAEA" 
    android:paddingBottom="8dp" 
    android:paddingLeft="5dp" 
    android:paddingRight="5dp" 
    android:paddingTop="8dp" /> 
    <RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#ffeaeaea" > 
    <View 
     android:layout_width="fill_parent" 
     android:layout_height="1dp" 
     android:layout_alignBottom="@+id/current" 
     android:background="#C5C5C5" /> 
    <ImageView 
     android:id="@+id/current" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerInParent="true" 
     android:src="@drawable/indicator_current" /> 
    </RelativeLayout> 
    <android.support.v4.view.ViewPager 
    android:id="@+android:id/viewpager" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 
</LinearLayout> 

main_pg0.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" 
    android:orientation="vertical" 
    android:id="@+id/linearLayout" > 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:text="main_pg0.xml" 
     android:textColor="#0C0C0C" 
     android:textSize="18sp" /> 

</LinearLayout> 

什么即时试图做的是编程方式添加一个文本视图,main_pg0.xml而已,而剩下的其他2页单独

+0

你应该充气/在'PagerAdapter'地方创建'ViewPager'内页的版面(S),所以这将是适当的地方添加视图布局。或者,你可以将'Fragment'提供给'ViewPager',通过这样做,你可以在相关的'Fragment'类中进行布局改变。 –

+0

@MH。我认为这正在缓慢推动我朝着正确的方向发展,但我仍然无法实现它的目标。听起来你有过这样的经历,有没有可能得到一个例子? – Badams

+0

好吧,我已经添加了一个快速示例,以回答您的问题。希望这会让事情更清楚。 –

回答

2

您还没有明确提到,无论您是喂Fragment S到ViewPager,但因为你是从Zylinc执行工作,我会假设你是。事实上,我只是使用提供的示例代码向您展示这个想法。

了解如何实现您所追求的内容的关键是要获得ViewPager中的页面是Fragment s形式的自包含单元。它们可重用,有自己的生命周期,并可以处理自己的布局。因此,如果您想对页面布局进行运行时修改,则需要在组成这些页面的Fragment(s)内执行此操作。

以下方法可以在示例代码的ItemFragment类中找到。我简单地将TextView从您自己的代码片段添加到它。为了演示目的,我确实隐藏了用于填充date_fragment.xml布局文件中所有空间的ListView

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.date_fragment, container, false); 
    View tv = v.findViewById(R.id.text); 
    ((TextView)tv).setText(readableDateFormat.format(date)); 

    // changes below 
    TextView valueTV = new TextView(getActivity()); 
    valueTV.setText("hallo hallo"); 
    valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
    ((ViewGroup)v).addView(valueTV); 

    return v; 
} 

这会给你的 “喂你好” 文本(你是一个Dutchie;?))的正下方在页面日期:

result of code snippet

顺便说一句,你可能想要考虑切换到杰克的ViewPagerIndicator。尽管我没有仔细看过你目前使用的那个,但Jake对我来说似乎更灵活,并且更加合作。当然是你。


编辑:好了,你实际上并没有使用在ViewPager片段,而是互相喂给它的布局。这很好,但当页面变得更复杂时,它可能会变得更难管理。无论如何,在运行时更改页面布局的关键仍然是在你构建/充气时做到这一点。当使用片段时,这将在覆盖的方法中,如上所示。然而,就你而言,你需要直接在instantiateItem(...)中完成。

我实际上没有在IDE中输入,所以请介意和小的语法错误。

public Object instantiateItem(View collection, int position) { 

    LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View view = null; 
    switch (position) { 
    case 0: 
     view = inflater.inflate(R.layout.main_pg0, null); 
     TextView valueTV = new TextView(getActivity()); 
     valueTV.setText("hallo hallo"); 
     valueTV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
     ((ViewGroup)view).addView(valueTV); 
     break; 
    case 1: 
     view = inflater.inflate(R.layout.main_pg1, null); 
     // or perhaps better for readability: build the layout in a different method, passing in the root 
     buildSecondPageLayout(view); 
     break; 
    case 2: 
     view = inflater.inflate(R.layout.main_pg2, null); 
     buildThirdPageLayout(view); 
     break; 
    } 

    return view; 
} 
+0

我真的很感激这个例子!我认为这让我走在正确的轨道上,但仍然没有运气。我添加了更多的信息给我的问题,包括我的代码。提供的示例的问题在于,textview显示在所有页面上,并且页面没有像我一样使用每个页面的xml布局。此外,我尝试了一个小时,今天早上让Jakes ViewPagerIndicator工作...虽然看起来更好,但是当我无法工作甚至导入项目时,我放弃了。 – Badams

+0

我很抱歉听到。添加的代码澄清了一些事情,所以请在我的答案中看到编辑。注意:如果我没有记错,您需要将Jake的ViewPagerIndicator设置为“新Android项目”>“从现有源...”;您不能直接导入项目。 –

+0

啊,是的!这正是我需要的!不用谢谢你的帮助@MH – Badams

2

您必须添加在主视图中是充气布局的视图。因此只需添加一行。

LayoutInflater factory = getLayoutInflater(); 

View layout = factory.inflate(R.layout.main_pg0, null); 
View linearLayout = layout.findViewById(R.id.linearLayout); 

TextView valueTV = new TextView(this); 
valueTV.setText("hallo hallo"); 
valueTV.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 

((LinearLayout) linearLayout).addView(valueTV); 
layout.addView(linearLayout); 
+0

感谢您的输入@Karn,但是我做到了这一点,并没有什么区别: – Badams

相关问题