Ich versuche, die benutzerdefinierte Navigationsleiste ohne Verwendung von Aktionsleiste und Symbolleiste zu entwickeln, wenn der Benutzer auf die Schaltfläche klickt, öffnet sich die Navigation.Wenn Sie eine Probe haben, teilen Sie bitteWie öffne ich die Navigationsleiste, wenn die Taste klickt? ohne Verwendung der Aktionsleiste in android
Antwort
Sie können verwenden drawerLayout.openDrawer (GravityCompat.START);
Implementierung
In Sie Aktivität
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
drawerLayout.openDrawer(GravityCompat.START);
}
});
XML für Navigationsansicht
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:fitsSystemWindows="true"
android:overScrollMode="always"
app:headerLayout="@layout/drawer_header"
app:itemBackground="@@android:color/white"
app:itemIconTint="@color/app_primary"
app:itemTextColor="@color/app_primary"
app:menu="@menu/drawer_menu">
Wenn Sie DrawerLayout
von Support-Bibliothek verwenden (innen Drawerlayout sein sollte) Sie es opneDrawer()
Methoden bekam :
Siehe docs: https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
Sie ein 'DrawerToggle' verwenden müssen, wie diese
//initialisation left drawer
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
mToolbar, R.string.app_name, R.string.app_name) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
//syncState();
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
//syncState();
}
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
Verwenden Sie die DrawerLayout
mDrawerLayout = (DrawerLayout) getView().findViewById(R.id.drawer_layout);
//To Open:
mDrawerLayout.openDrawer(Gravity.START);
//To Close:
mDrawerLayout.closeDrawer(Gravity.END);
dieses Beispiel
ein Fragment erstellen ist, NavigationDrawerFragment.class
public class NavigationDrawerFragment extends Fragment {
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private int mFragmentId;
private View mFragmentContainerView;
public interface EventDrawerListener {
void someEvent(String s);
}
private EventDrawerListener mEventDrawerListener;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.slide_menu, container, false);
return v;
}
public void setUp(int fragmentID, DrawerLayout drawerLayout, EventDrawerListener eventDrawerListener) {
this.mFragmentId = fragmentID;
this.mEventDrawerListener = eventDrawerListener;
mFragmentContainerView = getActivity().findViewById(mFragmentId);
mDrawerLayout = drawerLayout;
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, R.string.open, R.string.close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActivity().invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private ActionBar getActionBar() {
return ((AppCompatActivity) getActivity()).getSupportActionBar();
}
}
in activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/toolbar" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content Your Main"/>
</RelativeLayout>
<fragment
android:id="@+id/navigation_drawer"
android:name="your.package.NavigationDrawerFragment"
android:layout_width="@dimen/width_menu"
android:layout_height="match_parent"
android:layout_gravity="start"
android:splitMotionEvents="false"
tools:layout="@layout/slide_menu" />
</android.support.v4.widget.DrawerLayout>
in MainActivity.class
public class MainActivity extends Activity{
private NavigationDrawerFragment mNavigationDrawerFragment;
private DrawerLayout mDrawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpDrawer();
ImageView image=(ImageView) findViewById(R.id.image_home);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
} else {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
}
});
}
private void setUpDrawer() {
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mNavigationDrawerFragment.setUp(R.id.relative_category, (DrawerLayout) findViewById(R.id.drawer_layout), this);
mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
}
}
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="@color/color_bg_splash"
android:minHeight="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="@+id/image_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:gravity="center"
android:paddingBottom="@dimen/padding16dp"
android:paddingRight="@dimen/padding16dp"
android:paddingTop="@dimen/padding16dp"
android:src="@drawable/icon_menu" />
</android.support.v7.widget.Toolbar>
hoffen, dass es Ihnen helfen !!!
Sie legen DrawerLayout unter Meine Symbolleiste fest.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/toolbar" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_below="@+id/toolbar"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Content Your Main"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Ich versuche Navigationsleiste mit actionbar andtoolbar zu entwickeln @Nguyen Trung Hieu –
Ich aktualisiere meine Antwort !!!! –
Sie legen DrawerLayout unter Meine Symbolleiste fest. Navigationsschublade mit Aktionsleiste und Symbolleiste –
Bitte teilen Sie den Code von dem, was Sie schon gemacht haben? –
Ich weiß nicht über Naviagtion Schublade .. @AlokGupta –
Bitte lesen Sie online. –