我想通过创建视图对象的实例来为我的活动添加一个视图所以假设这是我的活动
public class ConverTo extends FragmentActivity
private LinearLayout l1;
private LengthFragment test;
private static final int RESULT_SETTINGS = 1;
@Override
public void onCreate(Bundle savedInstanceState) { //methods to call on app launch
super.onCreate(savedInstanceState);
setContentView(R.layout.converto_home);
pagerAdapter = new MainPagerAdapter();
pager = (ViewPager) findViewById (R.id.pager);
pager.setAdapter (pagerAdapter);
test = new LengthFragment(ConverTo.this);
l1 = (LinearLayout) getLayoutInflater().inflate(R.layout.view_temperature, null);
addView(l1);
当我这样做^^时,它会正确显示视图。而不是这样做^^我想这样做
LengthFragment l1= new LengthFragment(context,getLayoutInflater());
addView(l1);
并且我的 lengthfragment 类将自身关联到一个 xml 文件。
public class LengthFragment extends View implements OnClickListener, OnItemSelectedListener{
public LengthFragment(Context context, LayoutInflater inflater){
super(context);
rContext = context;
inflater.inflate(R.layout.view_length, null, false);
我试过这样做^^,它给我的只是一个空洞的观点。