##Fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/my_fragment_id"
android:name="com.example.day004.fragment.MyFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</fragment>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private TextView tvOne;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
MyFragment myFragment = new MyFragment();
fragmentTransaction.add(R.id.main_layout_id,myFragment);
fragmentTransaction.remove(myFragment);
OneFragment oneFragment = new OneFragment();
fragmentTransaction.replace(R.id.main_layout_id,oneFragment);
fragmentTransaction.hide(oneFragment);
fragmentTransaction.commit();
}
}
public class Main2Activity extends AppCompatActivity {
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
text = (TextView) findViewById(R.id.text);
registerForContextMenu(text);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.ceshi, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.blue:
text.setTextColor(Color.parseColor("#2239A2"));
break;
case R.id.green:
text.setTextColor(Color.parseColor("#1BA233"));
break;
case R.id.red:
text.setTextColor(Color.parseColor("#A21C31"));
break;
}
return super.onContextItemSelected(item);
}
}