我有一个viewpager,我正在显示一些文本。我还有一个按钮,当我点击它时,我想在viewpager中更改textview的TextSize。这是我的代码:
public class SrtSample extends Activity {
LayoutInflater inflater ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setBackgroundColor(0xff000000);
SlideSrt adapter = new SlideSrt(this);
viewPager.setAdapter(adapter);
Button b= (Button) findViewById(R.id.pgtpics);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Dont know what to write here.
// This is the code i am using here but its not working.
inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.MyLayout, null);
TextView tv = (TextView)vi.findViewById(R.id.tv);
tv.setTextSize(25);
}
});
}
}
这是viewpager的pagerAdapter类:
public class SlideSrt extends PagerAdapter {
Context context;
LayoutInflater inflater;
String s1 = "Text1";
String s2 = "Text2";
String s3 = "Text3";
String s4 = "Text4";
String s5 = "Text5";
String allStrings[] = {s1,s2,s3,s4,s5}
//Constructor
SlideSrt(Context context) {
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return allString.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((LinearLayout) object);
}
public Object instantiateItem(ViewGroup container, int position) {
View itemview;
itemview = inflater.inflate(R.layout.MyLayout, container, false);
TextView tv= (TextView) itemview.findViewById(R.id.tv);
tv.setText(allsrt[position]);
((ViewPager) container).addView(itemview, 0);
return itemview;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
}
当我点击按钮时,没有任何反应。 textSize保持不变。我必须在OnclickListener方法中做错事。或者必须有其他一些方法。请帮我。我一直试图解决这个问题,但没有运气。
答案 0 :(得分:1)
在onClick方法中,您正在膨胀R.layout.MyLayout
,但您没有将其附加到可见父级。所以你的TextView实际上并不可见。而不是给布局充气,请在包含TextView的视图上调用findViewById
。