我正在尝试从右到左制作import UIKit
class ViewController: UIViewController {
@IBOutlet weak var lblGetMsg: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func btnGetMsg(sender: AnyObject) {
let url = NSURL(string: "http://ip.jsontest.com")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
print(NSString(data: data!, encoding: NSUTF8StringEncoding))
self.lblGetMsg.text=(String(data: data!, encoding: NSUTF8StringEncoding))
}
task.resume()
}
}
幻灯片。
我已经关注this tutorial,发现从左到右滑动的DrawerLayout
非常令人满意。
当我将DrawerLayout
的{{1}}更改为gravity
时,它会从右向左滑动,但是,汉堡按钮不再正常工作,它会显示{{1}当我试图触摸它时。
这是修改后的切换功能
android.support.design.widget.NavigationView
那么..如何让汉堡按钮再次工作?
更新
这是主要布局的XML文件
right
这是我的主要课程
No drawer view found with gravity LEFT
2016年2月2日更新
尝试将抽屉视图更改为// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
//Added the lines below, tried solutions online but fail..
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
} else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
}
return false;
}
};
..但这是一个坏主意,因为没有任何变化。试着将<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".Activities.NewsfeedActivity">
<LinearLayout
android:id="@+id/activity_newsfeed_wrapper"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical" >
<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/activity_newsfeed_toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/abc_action_bar_default_height_material"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
<FrameLayout
android:id="@+id/activity_newsfeed_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/activity_newsfeed_navigationView"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="right"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/drawer" />
</android.support.v4.widget.DrawerLayout>
移到public class NewsfeedActivity extends AppCompatActivity {
//Defining Variables
private Toolbar toolbar;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newsfeed);
// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.activity_newsfeed_toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.activity_newsfeed_navigationView);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
//Replacing the main content with ContentFragment Which is our Inbox View;
case R.id.a:
Toast.makeText(getApplicationContext(), "Inbox Selected", Toast.LENGTH_SHORT).show();
ContentFragment fragment = new ContentFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.activity_newsfeed_frame,fragment);
fragmentTransaction.commit();
return true;
// For rest of the options we just show a toast on click
case R.id.b:
Toast.makeText(getApplicationContext(),"b Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.c:
Toast.makeText(getApplicationContext(),"c Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.d:
Toast.makeText(getApplicationContext(),"d Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.e:
Toast.makeText(getApplicationContext(),"e Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.f:
Toast.makeText(getApplicationContext(),"f Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.g:
Toast.makeText(getApplicationContext(),"g Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.h:
Toast.makeText(getApplicationContext(),"h Selected",Toast.LENGTH_SHORT).show();
return true;
default:
Toast.makeText(getApplicationContext(),"Somethings Wrong",Toast.LENGTH_SHORT).show();
return true;
}
}
});
// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.drawer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case android.R.id.home:
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
} else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
return true;
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
}
,更糟糕的是因为没有滑动菜单..
尝试改变RIGHT
NavigationView
将LinearLayout
更改为onOptionsItemSelected
,“NOPE”,代码说明了......
答案 0 :(得分:1)
在导航视图中尝试此操作
机器人:layout_gravity =&#34;端&#34;取代android:layout_gravity =&#34; right&#34;
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="end"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
/>
//在java活动文件中添加这些行,它将起作用
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (mDrawer.isDrawerOpen(Gravity.RIGHT)) {
mDrawer.closeDrawer(Gravity.RIGHT);
} else {
mDrawer.openDrawer(Gravity.RIGHT);
}
return true;
case R.id.action_settings:
return true;
}
final update:
我更改了示例项目中的一些行(MainActivity.java),现在它正在运行,使用此代码。
别忘了写android:layout_gravity =&#34; end&#34;代替android:layout_gravity =&#34; start&#34;在你的xml文件中。
package com.android4dev.navigationview;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
NavigationView navigationView;
DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolbar);
navigationView=(NavigationView)findViewById(R.id.navigation_view);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer);
// Set a Toolbar to replace the ActionBar.
setToolbarAsActionBar();
// Setup drawer view
setupDrawerContent(navigationView);
// Set the menu icon instead of the launcher icon.
final ActionBar ab = getSupportActionBar();
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
ab.setDisplayHomeAsUpEnabled(true);
//ab.setDisplayShowTitleEnabled(false);
Menu menu = navigationView.getMenu();
MenuItem item = menu.findItem(R.id.starred);
selectDrawerItem(item);
}
private void setToolbarAsActionBar() {
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (drawerLayout.isDrawerOpen(Gravity.RIGHT)) {
drawerLayout.closeDrawer(Gravity.RIGHT);
} else {
drawerLayout.openDrawer(Gravity.RIGHT);
}
return true;
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the planet to show based on
// position
switch (menuItem.getItemId()) {
case R.id.inbox:
Toast.makeText(getApplicationContext(), "Inbox Selected", Toast.LENGTH_SHORT).show();
ContentFragment fragment = new ContentFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, fragment);
fragmentTransaction.commit();
break;
case R.id.starred:
Toast.makeText(getApplicationContext(), "Stared Selected", Toast.LENGTH_SHORT).show();
break;
case R.id.sent_mail:
Toast.makeText(getApplicationContext(), "Send Selected", Toast.LENGTH_SHORT).show();
break;
case R.id.drafts:
Toast.makeText(getApplicationContext(), "Drafts Selected", Toast.LENGTH_SHORT).show();
break;
case R.id.allmail:
Toast.makeText(getApplicationContext(), "All Mail Selected", Toast.LENGTH_SHORT).show();
break;
case R.id.trash:
Toast.makeText(getApplicationContext(), "Trash Selected", Toast.LENGTH_SHORT).show();
break;
case R.id.spam:
Toast.makeText(getApplicationContext(), "Spam Selected", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
break;
}
drawerLayout.closeDrawers();
}
private ActionBarDrawerToggle setupDrawerToggle() {
return new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer);
}
}
答案 1 :(得分:0)
对于仍然存在此问题的任何人: 您应该在NavigationView中设置以下行:
android:layout_gravity="start"
,这在您的Java中:
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close){
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item != null && item.getItemId() == android.R.id.home){
if (drawerLayout.isDrawerOpen((Gravity.END))) {
drawerLayout.closeDrawer(Gravity.END);
} else {
drawerLayout.openDrawer(Gravity.END);
}
}
return false;
}
};
对我有用。
答案 2 :(得分:0)
val drawerLayout:DrawerLayout = findViewById(R.id.drawer_layout)
val navView:NavigationView = findViewById(R.id.nav_view)
val navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(
setOf(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow
), drawerLayout
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
val drawertoogle = ActionBarDrawerToggle (this,drawerLayout,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawertoogle.isDrawerIndicatorEnabled = false
drawertoogle.setToolbarNavigationClickListener
drawerLayout.openDrawer(GravityCompat.END)
}