我已经尝试了大约一个星期来让我的 PagerView 不与 TitleStrip 重叠。
我已经尝试了我能想到的一切,一个看起来像同一个问题的 StackOverflow 问题的答案不适用。
看来 PagerTitleStrip 和 TextView 都从 0,0(左,上)开始
任何帮助,将不胜感激。请注意,我不能使用任何 XML(inc Layout.xml),所以这一切都是以编程方式完成的。
这是我的问题的完整工作示例:
package com.example.projname;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
PagerTabStrip myPagerTabStrip = new PagerTabStrip(this);
myPagerTabStrip.setGravity(Gravity.TOP);
ViewPager viewPager = new ViewPager(this);
viewPager.addView(myPagerTabStrip, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
viewPager.setAdapter(myPagerAdapter);
setContentView(viewPager);
}
class MyPagerAdapter extends PagerAdapter {
public final String[] Titles = {
"Title One",
"Title Two",
"Title Three",
"Title Four",
"Title Five"
};
@Override
public int getCount() {
return Titles.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
@Override
public CharSequence getPageTitle(int position) {
return Titles[position];
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
TextView textView = new TextView(getApplicationContext());
String myString = new String("Page " + (position + 1) + "\r\n");
textView.setText(myString + myString + myString + myString + myString + myString);
container.addView(textView);
return textView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
Edit1:添加问题的屏幕截图:
Edit2:用答案更新问题(以防将来有人尝试这样做
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutParams layoutParams = new LayoutParams();
layoutParams.height = LayoutParams.WRAP_CONTENT;
layoutParams.width = LayoutParams.MATCH_PARENT;
layoutParams.gravity = Gravity.TOP;
PagerTabStrip myPagerTabStrip = new PagerTabStrip(this);
MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
ViewPager viewPager = new ViewPager(this);
viewPager.addView(myPagerTabStrip, layoutParams);
viewPager.setAdapter(myPagerAdapter);
setContentView(viewPager);
}