XML:
<android.support.v4.widget.DrawerLayout
android:id="@+id/draw"
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:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment"
android:layout_width="300dp"
android:layout_height="match_parent"
class="guopuran.bwie.com.cehua.Fragmentone"
android:layout_gravity="start"
/>
</android.support.v4.widget.DrawerLayout>
fragmentonexml:
<TextView
android:layout_width="300dp"
android:layout_height="match_parent"
android:text="11111"/>
values文件夹中strings.xml:
<resources>
<string name="app_name">cehua</string>
<string name="kai">kai</string>
<string name="guan">guan</string>
</resources>
Fragment类:
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragmentone,container,false);
return view;
}
Activity:
private DrawerLayout drawerLayout;
private ActionBar actionBar;
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取资源ID
drawerLayout = findViewById(R.id.draw);
actionBar = getSupportActionBar();
//顶部的开启按钮
actionBar.setDisplayHomeAsUpEnabled(true);
toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.kai, R.string.guan);
//同步
toggle.syncState();
drawerLayout.addDrawerListener(toggle);
}
//点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}