java在Android中以编程方式添加视图
是否可以将视图添加到与OnCreate()中的setContentView()中调用的布局不同的布局
我正在尝试使用Zylincs ViewPager(在http://www.zylinc.com/blog-reader/items/viewpager-page-indicator.html找到),我有这个部件的设置,并且工作得很好
这给我留下的是4个布局。1是主要的。xml,其他三个是main_pg0。xml,main_pg1。xml和main_pg3。xml
这三个页面是每个“页面”的内容的主要位置。xml包含页面查看器
在onCreate im中,使用setContentView(main.xml)
我现在想做的是能够通过编程将文本视图添加到一些页面中
我现在得到的是:
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:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:orientation="vertical" >
<com.zylinc.view.ViewPagerIndicator
安卓:id="@+id/indicator"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:background="#EAEAEA"
安卓:paddingBottom="8dp"
安卓:paddingLeft="5dp"
安卓:paddingRight="5dp"
安卓:paddingTop="8dp" />
<RelativeLayout
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:background="#ffeaeaea" >
<View
安卓:layout_width="fill_parent"
安卓:layout_height="1dp"
安卓:layout_alignBottom="@+id/current"
安卓:background="#C5C5C5" />
<ImageView
安卓:id="@+id/current"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentTop="true"
安卓:layout_centerInParent="true"
安卓:src="@drawable/indicator_current" />
</RelativeLayout>
<安卓.support.v4.view.ViewPager
安卓:id="@+安卓:id/viewpager"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent" />
</LinearLayout>
main_pg0。xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:background="#FFFFFF"
安卓:orientation="vertical"
安卓:id="@+id/linearLayout" >
<TextView
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:layout_marginLeft="5dp"
安卓:text="main_pg0.xml"
安卓:textColor="#0C0C0C"
安卓:textSize="18sp" />
</LinearLayout>
我试图做的是以编程方式向main_pg0添加一个文本视图。仅限xml,而其他两页则单独保留
# 1 楼答案
您没有明确提到是否将
Fragment
馈送到ViewPager
,但是由于您是从Zylinc实现工作的,我假设您是。事实上,我将使用提供的示例代码向您展示这个想法理解如何实现您所追求的目标的关键是要确保
ViewPager
中的页面是以Fragment
形式的自包含单元。它们是可重用的,有自己的生命周期,并且可以处理自己的布局。因此,如果您想对页面的布局进行运行时修改,那么您需要在组成这些页面的Fragment
内部进行修改在示例代码的
ItemFragment
类中可以找到以下方法。我只是从您自己的代码片段中添加了TextView
。出于演示目的,我确实隐藏了用于填充ListView
布局文件中所有空间的date_fragment.xml
这将给你“哈罗哈罗”文本(你是达奇?;)在页面中日期的正下方:
顺便说一下,您可能需要考虑切换到卫国明的^{Edit:好的,因此您实际上没有在
ViewPager
中使用片段,而是将布局提供给它。这很好,不过当页面变得更复杂时,可能会变得更难管理。无论如何,在运行时更改页面布局的关键仍然是在构建/膨胀页面时进行更改。当使用片段时,它将位于重写的方法中,如上所示。但是,在您的情况下,您需要直接在instantiateItem(...)
中执行此操作我实际上并没有在IDE中输入这个,所以请注意一些小的语法错误
# 2 楼答案
您必须在主视图中添加视图,主视图是充气布局。所以只需添加一行