是否可以将 View 添加到与在 OnCreate() 的 setContentView() 中调用的布局不同的布局?
我正在尝试使用 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 包含页面查看器。
在 onCreate im 中使用 setContentView(main.xml)。
我现在想做的是能够以编程方式将 textView 添加到某些页面。
我现在拥有的是:
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 。
----编辑----
希望能更清楚地说明我想做什么 这是我的代码
主.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 添加一个 TextView ,而单独保留其他 2 个页面
最佳答案
您没有明确提到是否将 Fragment
提供给 ViewPager
,但由于您是从 Zylinc 实现开始工作,我将假设你是。事实上,我将使用提供的示例代码向您展示这个想法。
了解如何实现您所追求的目标的关键是让 ViewPager
中的页面是 Fragment
形式的自包含单元。它们是可重用的,有自己的生命周期,可以处理自己的布局。因此,如果您想对页面布局进行运行时修改,您需要在构成这些页面的 Fragment
(s) 中执行此操作。
可以在示例代码的 ItemFragment
类中找到以下方法。我只是将您自己的代码 fragment 中的 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;
}
这将在页面中的日期正下方为您提供“hallo hallo”文本(您是荷兰人?;)):
顺便说一句,您可能要考虑切换到 Jake 的 ViewPagerIndicator
.虽然我没有仔细查看您当前使用的那个,但 Jake 的对我来说似乎更灵活,使用起来更干净。当然取决于你。
编辑:好的,您实际上并没有在 ViewPager
中使用 fragment ,而是为其提供了布局。这很好,尽管当页面变得更复杂时它可能会变得更难管理。无论如何,在运行时更改页面布局的关键仍然是在构建/扩充它时进行。使用 fragment 时,将在重写方法中,如上所示。但是,对于您的情况,您需要直接在 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;
}
关于java - 在 Android 中以编程方式添加 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10660111/