关于activity_main.xml与fragment_main.xml

分享到:

第一种解决办法

新版安装SDK文件一开始有两个XML文件,activity_main.xml和fragment_main.xml,
不习惯的可以这样处理:
1.删除fragment_main.xml整个文件
2.对activity_main.xml,删除里面的内容。然后切换到Graphy Layout,放入一个LinearLayout就可以
3.对MainActivity.java,可以删除部分的内容,再把MainActivity extends ActionBarActivity 改为MainActivity extends Activity :
改后如下:
package com.zmcorp.weightcalculator;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}


}

}

第二种方法::

myTextview,myButton是在fragment_main.xml中的,在没引进fragment_main.xml之前,你是通过findViewById找不到的。。

public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView textview = (TextView)rootView.findViewById(R.id.view);
System.out.println(textview);
testview.setText("Hello Android!");

return rootView;
}
}

昵    称:
验证码:

相关文档: