我有许多共享相同导航抽屉的活动。如何使用相同的导航抽屉为所有活动编写不同的视图。 xml和Java文件可以在下面找到。
navigation_drawer.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<LinearLayout
android:id="@+id/navigationLinearLayout"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical" >
<ListView
android:id="@+id/navList_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|start"
android:choiceMode="singleChoice"
android:listSelector="@color/notificationarea_background"
android:background="@color/navigation_background"/>
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@color/notificationarea_background"/>
<ListView
android:id="@+id/navList_two"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="@color/navigation_background"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
import android.os.Bundle;
public class MainActivity extends NavigationDrawer {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.navigation_drawer, frameLayout);
}
}
Login.java
import android.os.Bundle;
public class Login extends NavigationDrawer {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.navigation_drawer, frameLayout);
}
}
navigation_drawer_text.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv"
android:textColor="@color/navigation_text"
android:padding="18dp"
android:layout_width="fill_parent"
android:background="@drawable/navigation_selector"
android:singleLine="true"
android:gravity="left"
android:layout_height="fill_parent"/>
NavigationDrawer.java
public class NavigationDrawer extends AppCompatActivity {
protected FrameLayout frameLayout;
public ListView mDrawerList_one, mDrawerList_two;
private ArrayAdapter<String> mAdapter_one, mAdapter_two;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private String mActivityTitle;
//Just the sample links.
String[] Links1 = { "Login1", "Login2", "MainActivity1" };
String[] Links2 = { "Login3", "Login4", "MainActivity2" };
protected int position_itemSelected; //Position of item selected in ListView
private static boolean isLaunch = true;
SharedPreferences itemSelected_shared;
SharedPreferences.Editor itemSelected_editor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
frameLayout = (FrameLayout)findViewById(R.id.content_frame);
mDrawerList_one = (ListView)findViewById(R.id.navList_one);
mDrawerList_two = (ListView)findViewById(R.id.navList_two);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
itemSelected_shared = getApplicationContext().getSharedPreferences("Item Selected in Navigation Drawer", 0);
itemSelected_editor = itemSelected_shared.edit();
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.notificationarea_background));
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void addDrawerItems() {
mAdapter_one = new ArrayAdapter<String>(this, R.layout.navigation_drawer_text, Links);
mDrawerList_one.setDivider(null);
mDrawerList_one.setAdapter(mAdapter_one);
if(itemSelected_shared.getInt("Item Selected", 0) == 1) {
mDrawerList_one.post(new Runnable() {
@Override
public void run() {
mDrawerList_one.setSelected(true);
mDrawerList_one.getChildAt(itemSelected_shared.getInt("Item Selected One", 0)).setBackgroundColor(getResources().getColor(R.color.notificationarea_background));
}
});
}
mAdapter_two = new ArrayAdapter<String>(this, R.layout.navigation_drawer_text, Links);
mDrawerList_two.setDivider(null);
mDrawerList_two.setAdapter(mAdapter_two);
if(itemSelected_shared.getInt("Item Selected", 0) == 2) {
mDrawerList_two.post(new Runnable() {
@Override
public void run() {
mDrawerList_two.setSelected(true);
mDrawerList_two.getChildAt(itemSelected_shared.getInt("Item Selected Two", 0)).setBackgroundColor(getResources().getColor(R.color.notificationarea_background));
}
});
}
mDrawerList_one.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
itemSelected_editor.putInt("Item Selected", 1);
itemSelected_editor.putInt("Item Selected One", position);
itemSelected_editor.commit();
}
});
mDrawerList_two.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
itemSelected_editor.putInt("Item Selected", 2);
itemSelected_editor.putInt("Item Selected Two", position);
itemSelected_editor.commit();
if (isLaunch) {
switch (position) {
case 0:
//Intent, which is working fine.
case 1:
//Intent, which is working fine.
}
}
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open, R.string.drawer_close) {
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
我正在我的其他活动(MainActivity和Login)中扩展NavigationDrawer。 我希望MainActivity和Login具有导航抽屉以及他们自己的xml视图/ xml文件。如果我在navigation_drawer.xml中放入一些内容,那么它将在所有活动中出现。我希望MainActivity和Login具有相同的导航抽屉的不同内容。
谢谢。
答案 0 :(得分:0)
每个Activity必须有一个片段布局并从那里进行充气。看一下this教程,它真的很棒:
答案 1 :(得分:0)
尝试这种方式来创建滑动抽屉,它会有所帮助
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<balaji.slidingdrawer_tb.Transparent
android:id="@+id/popup_window"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:padding="30px">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="20dp"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:text="Top to Bottom Sliding" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:text="Jelly Bean" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:text="Ice Cream Sandwich" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:text="HoneyComb" />
</balaji.slidingdrawer_tb.Transparent>
<Button
android:id="@+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Handle"/>
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
CheckBox cb1,cb2,cb3;
int key=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Transparent popup = (Transparent) findViewById(R.id.popup_window);
popup.setVisibility(View.GONE);
final Button btn=(Button)findViewById(R.id.handle);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if(key==0){
key=1;
popup.setVisibility(View.VISIBLE);
// btn.setBackgroundResource(R.drawable.ic_launcher);
}
else if(key==1){
key=0;
popup.setVisibility(View.GONE);
// btn.setBackgroundResource(R.drawable.ic_action_search);
}
}
});
}
}
有关详细信息,请参阅here