我正在设计支持库,我想在不同的活动中使用“相同的导航抽屉”。我尝试使用以下代码扩展MainActivity,但不起作用。
MainActivity.java
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawer = (NavigationView) findViewById(R.id.main_drawer);
mDrawer.setNavigationItemSelectedListener(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private void navigate(int mSelectedId) {
Intent intent = null;
if (mSelectedId == R.id.navigation_item_2) {
mDrawerLayout.closeDrawer(GravityCompat.START);
intent = new Intent(this, SampleActivity.class);
startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
SampleActivity.java
public class SecondActivity extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml中
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/main_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:itemIconTint="@color/colorAccent"
app:itemTextColor="@color/colorTextSecondary"
app:menu="@menu/menu_drawer" />
答案 0 :(得分:4)
它无法正常工作,因为在SampleActivity
中您通过调用以下内容来覆盖内容视图:
setContentView(R.layout.activity_second);
通过拨打该号码,您的SampleActivity
将失去对NavigationView
的访问权限,因为setContentView()
已更改布局,而不是将其扩展到您的MainActivity&#39; s布局。
解决这个问题的方法是提供一个专门的地方(比如FrameLayout
)然后膨胀任何子类&#39;布局到它。
// Add these codes below the toolbar in your activity_main.xml
<FrameLayout
android:id="@+id/ly_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
在扩展课程时,请调用此课程而不是setContentView()
:
getLayoutInflater().inflate(R.layout.activity_second, findViewById(R.id.ly_content));
答案 1 :(得分:0)
使用 getLayoutInflater()。inflate(R.layout.activity_second,main_content);
而不是setContentView(R.layout.activity_second);
您可以在http://androiddeveloperdemo.blogspot.in/2014/08/android-navigation-drawer-with-multiple.html
找到工作示例答案 2 :(得分:0)
使用抽象方法让布局膨胀: 在MainActivity中:
Mage::log()
在SampleActivity中:
public abstract int getLayoutId(){}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
//Other stuff
}
请记住在您的activity_second.xml
中添加public int getLayoutId(){
return R.layout.activity_second);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Other stuff, NO setContentView();
}
和Toolbar