我创建了三个活动探索/交易和包。我使用抽屉布局创建了菜单。我想在每个活动屏幕上看到我的菜单图标。我已经为菜单项创建了一个基本活动,并将其扩展到所有活动。我附上了我的家庭活动代码,基本活动代码并探索了下面的活动代码
package com.example.windows81.travelspoc;
import android.content.Context;
import android.content.Intent;
import android.support.design.widget.NavigationView;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import java.util.Timer;
import java.util.TimerTask;
public class HomeActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{
ViewPager viewPager;
LinearLayout sliderDotspanel;
NavigationView mNavigationView;
private int dotscount;
private ImageView[] dots;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mtoggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mtoggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open,
R.string.close);
NavigationView mNavigationView = (NavigationView)
findViewById(R.id.navigation);
mNavigationView.setNavigationItemSelectedListener(this);
mDrawerLayout.addDrawerListener(mtoggle);
mtoggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewPager);
sliderDotspanel = (LinearLayout) findViewById(R.id.SliderDots);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
viewPager.setAdapter(viewPagerAdapter);
Timer timer = new Timer();
timer.scheduleAtFixedRate(new MyTimerTask(), 2000, 4000);
dotscount = viewPagerAdapter.getCount();
dots = new ImageView[dotscount];
for (int i = 0; i < dotscount; i++){
dots[i] = new ImageView(this);
dots[i].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
R.drawable.incative_dot));
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(8, 0, 8,0);
sliderDotspanel.addView(dots[i], params);
}
dots[0].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
R.drawable.ic_active));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int
positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for (int i = 0; i < dotscount; i++){
dots[i].setImageDrawable(ContextCompat.getDrawable(getApplicationContext(),
R.drawable.incative_dot));
}
dots[position].setImageDrawable(ContextCompat.getDrawable
(getApplicationContext(), R.drawable.ic_active));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.navigation_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mtoggle.onOptionsItemSelected(item)){
return true;
}
switch (item.getItemId()) {
case R.id.explore:
// do what you want here
myMethod();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public class MyTimerTask extends TimerTask{
@Override
public void run() {
HomeActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (viewPager.getCurrentItem() == 0){
viewPager.setCurrentItem(1);
}else if (viewPager.getCurrentItem() == 1){
viewPager.setCurrentItem(2);
}else{
viewPager.setCurrentItem(0);
}
}
});
}
}
public void myMethod(){
Intent intent = new Intent(this,ExploreActivity.class);
startActivity(intent);
}
public void exploreActivity(View view){
myMethod();
}
public void packagesActivity(View view){
Intent intent = new Intent(this,PackagesActivity.class);
startActivity(intent);
}
public void dealsActivity(View view){
Intent intent = new Intent(this,DealsActivity.class);
startActivity(intent);
}
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.explore) {
myMethod();
}
return false;
}
}
这是我的家庭活动代码
package com.example.windows81.travelspoc;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
/**
* Created by Windows 8.1 on 11/15/2017.
*/
public class BaseActivity extends AppCompatActivity{
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.navigation_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.explore:
myMethod();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void myMethod(){
Intent intent = new Intent(this,ExploreActivity.class);
startActivity(intent);
}
public void exploreActivity(View view){
myMethod();
}
public void packagesActivity(View view){
Intent intent = new Intent(this,PackagesActivity.class);
startActivity(intent);
}
public void dealsActivity(View view){
Intent intent = new Intent(this,DealsActivity.class);
startActivity(intent);
}
}
这是我的基本活动代码
$(document).ready(function() { //function get executed when page finished all loading (i.e. html, JS/CSS files)
$("#getMessage").on("click", function() { //clicking on an element (can be button) with ID "getMessage" triggers this function
$.getJSON("/json/cats.json", function(json) { //Jquery method to fetch a file from given address and expected file should be a JSON file
var html = "";
// Only change code below this line.
json.forEach(function(val){ // on successfully getting the file this function would be triggered
var keys = Object.keys(val); //storing keys to a variable "keys"
html+="<div class='cat'>"; //appending HTML object to a var
keys.forEach(function(key){ // loop through all the keys
html+="<strong>"+key+"</strong>;"+val[key]+"<br>"; // add all the keys as strong text and place values next to them
});
html+="</div><br>"; // close the div and append line break
});
// Only change code above this line.
$(".message").html(html); //append the newly created HTML to a section (can be div) with ID "message" to show up on HTML page.
});
});
});
答案 0 :(得分:1)
如果您在所有活动中都有相同的菜单,那么我建议您创建一个
BaseActivity 然后将其扩展为 AppcompatActivity
制作活动1 - &gt;扩展 BaseActivity
魔术来到这里 - &gt;您不必一直使用setContentView。
使用One XML制作 BaseActivity - &gt; FrameLayout - &gt;与你的
ToolBar&amp;的FrameLayout
现在在 Activity1 中,在 BaseActivity 的 FrameLayout 中展开您的观点
对所有活动都遵循相同的系统。
我有同样的情况 - &gt;我必须在每个活动中显示sideMenu,所以我最终得到了这个解决方案。
答案 1 :(得分:0)
创建一个基本活动,在其中您编写菜单项的代码,然后将基本活动扩展到您的所有内部活动。
标题
创建一个通用的标题布局。
然后在你的HomeActivity中
abstract class HomeActivity extends Activity {
protected TextView txtHeading;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected void setHeading(int resId) {
if(tv_title == null)
txtHeading = findViewById(R.id.tv_title);
if(tv_title != null)
txtHeading.setText(resId);
}
}
然后在其他活动中,例如activity_main.xml
MainActivity
class MainActivity extends BaseActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setHeading(R.string.heading_main);
}
}
同样适用于其他活动
对于相同的菜单选项
public class HomeActivity extends Activity {
// other overidden methods
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item:
// do what you want here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
然后在儿童活动中
public class ChildActivity extends BaseActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
//getMenuInflater().inflate(R.menu.menu_second, menu); <- remove this
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}