我有导航抽屉菜单和6个活动。我想创建一个抽象活动类,并将所有抽屉菜单代码移动到它,然后扩展它。
这是我的主要activity.java和我在此活动中的抽屉代码。
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
protected DrawerLayout drawerLayout;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
TextView tv = (TextView)findViewById(R.id.textView);
Typeface textfont = Typeface.createFromAsset(getAssets(),"fonts/B Roya_YasDL.com.ttf");
tv.setTypeface(textfont);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.a1);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_more_vert_black_24dp);
navigationView = (NavigationView) findViewById(R.id.a2);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
item.setChecked(true);
drawerLayout.closeDrawer(Gravity.RIGHT);
switch (item.getItemId()){
case R.id.intro:
Intent i = new Intent(MainActivity.this, IntroActivity.class);
startActivity(i);
break;
}
return false;
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.khat) {
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
}
else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
return super.onOptionsItemSelected(item);
}
else {
Toast.makeText(getApplicationContext(),"somthing",Toast.LENGTH_LONG).show();
}
return true;
}
public void onBackPressed(){
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)){
drawerLayout.closeDrawer(Gravity.RIGHT);
}else {super.onBackPressed();}
}
这是我的第二个活动,例如:
public class IntroActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intro);
}
如何创建抽象活动类并在其中插入抽屉的代码并从我想要的任何活动中调用它?
答案 0 :(得分:1)
您必须创建类似
的mainActivitypublic class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
public NavigationView navigationView;
public DrawerLayout drawer;
public RelativeLayout content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
drawer=(DrawerLayout) findViewById(R.id.drawer_layout);
content = (RelativeLayout) findViewById(R.id.content);
navigationView = (NavigationView)findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(layoutResID, null);
// var x = layout.LayoutParameters;
content.addView(layout);
}
public void OpenDrawer()
{
drawer.openDrawer(Gravity.START,true);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.navdrawer_item_first:
Toast.makeText(this, "first item clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.navdrawer_item_second:
Toast.makeText(this, "second item clicked", Toast.LENGTH_SHORT).show();
break;
}
drawer.closeDrawers();
return true;
}
}
和activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:drawer="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
drawer:menu="@menu/navigation_drawer_items"
drawer:headerLayout="@layout/drawer_header"
android:background="#343a46"
drawer:itemTextColor="#ffffff" />
</android.support.v4.widget.DrawerLayout>
然后你将扩展邮件活动
public class SecondActivity extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_second);
}
}
并且在Manifest中,第二个活动是启动器:
<activity android:name=".MainActivity">
</activity>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>s
</activity>
答案 1 :(得分:0)
我不建议为此目的进行抽象活动 相反,您应该只有一个活动,其中附加了导航抽屉。并将活动替换为除包含导航抽屉的MainActivity之外的碎片。
activity_main
布局如下:
-DrawerLayout
-FrameLayout
-NavigationView
在主要活动中,只要用户点击NavigationDrawer中的任何项目,只需将片段替换为所需的片段。
答案 2 :(得分:0)
navigation_drawer_items
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/group_feature"
android:checkableBehavior="single">
<item android:id="@+id/navdrawer_item_first"
android:title="first item"/>
<item android:id="@+id/navdrawer_item_second"
android:title="second item"/>
</group>
</menu>
drawer_header
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#343a46"
android:orientation="vertical"
android:padding="10dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textColor="#dfba6b"
android:textStyle="bold" />
</LinearLayout>