2015-09-30 26 views
2

我需要在我的Android应用程序中添加导航抽屉菜单。我的应用程序有很多模块,所以我不能编码每个活动来显示导航菜单。所以我决定把导航菜单代码放在我的基本活动中。所以每项活动都会扩展此基本活动。导航抽屉菜单正常工作,但问题是活动组件无法正常工作。我不知道发生了什么事。需要改变吗?提前致谢。常见导航菜单抽屉在Android中

baseActivity.java 
public class BaseActivity extends ActionBarActivity { 
RelativeLayout fullLayout; 
DrawerLayout dLayout; 

@Override 
public void setContentView(int layoutResID) { 
    fullLayout = (RelativeLayout) getLayoutInflater().inflate(
      R.layout.activity_base, null); 
    FrameLayout frameLayout = (FrameLayout) fullLayout 
      .findViewById(R.id.content_frame); 
    ListView dList = (ListView) fullLayout.findViewById(R.id.left_drawer); 
    dLayout = (DrawerLayout) fullLayout.findViewById(R.id.drawer_layout); 
    getLayoutInflater().inflate(layoutResID, frameLayout, true); 
    setContentView(fullLayout); 

    String[] menu = new String[] { "Home", "Android", "Windows", "Linux", 
      "Raspberry Pi", "WordPress", "Videos", "Contact Us" }; 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, menu); 

    dList.setAdapter(adapter); 
    dList.setSelector(android.R.color.holo_blue_dark); 

    dList.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View v, int position, 
       long id) { 
      dLayout.closeDrawers(); 
     } 
    }); 
    }}; 

activity_base.xml

<RelativeLayout xmlns:tools="http://schemas.android.com/tools" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="sas.mobi.lakshmi.main.BaseActivity" > 

<FrameLayout 
    android:id="@+id/content_frame" 
    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" > 

    <ListView 
     android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#fff" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" /> 
</android.support.v4.widget.DrawerLayout> 

homeActivity.java

public class HomeAndSettingActivity extends BaseActivity { 
private Button btnAccount; 
private Button btnCollection; 
private Button btnOthers; 
private Button btnTempExit; 
private Button btnExit; 
private AdminDS adminDS; 
private AdminDO adminDO; 
private FinanceDS financeDS; 
private PartnerPaymentDS paymentDS; 
private GoogleCloudMessaging gcm; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home_and_setting); 
    initializeComponents(); 
    btnAccount.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getApplicationContext(), 
         AccountRegHomeActivity.class); 
       startActivity(intent); 
       finish(); 
      } 
     }); 
     btnCollection.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getApplicationContext(), 
         CollectionHomeActivity.class); 
       startActivity(intent); 
       finish(); 
      } 
     }); 
     btnOthers.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(getApplicationContext(), 
         OthersHomeActivity.class); 
       startActivity(intent); 
       finish(); 
      } 
     }); 
     btnTempExit.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       finish(); 
      } 
     }); 
     btnExit.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       financeDS = new FinanceDS(getApplicationContext()); 
       boolean isUps = financeDS.closeActiveCurrentFinances(); 
       if (isUps) { 
        finish(); 
       } 
      } 
     }); 

} 

/** 
* method to initialize components 
*/ 
private void initializeComponents() { 
    btnAccount = (Button) findViewById(R.id.btnAccount); 
    btnCollection = (Button) findViewById(R.id.btnCollection); 
    btnOthers = (Button) findViewById(R.id.btnOthers); 
    btnTempExit = (Button) findViewById(R.id.btnTempExit); 
    btnExit = (Button) findViewById(R.id.btnExit); 
}}; 

acitivity_home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@android:color/white" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/btnAccount" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="1dp" 
    android:background="?android:attr/dividerVertical" 
    android:text="@string/btnAccount" 
    android:textSize="14sp" /> 

<Button 
    android:id="@+id/btnCollection" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="1dp" 
    android:background="?android:attr/dividerVertical" 
    android:text="@string/btnCollection" 
    android:textSize="14sp" /> 

<Button 
    android:id="@+id/btnOthers" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="1dp" 
    android:background="?android:attr/dividerVertical" 
    android:text="@string/btnOthers" 
    android:textSize="14sp" /> 

<Button 
    android:id="@+id/btnTempExit" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="1dp" 
    android:background="?android:attr/dividerVertical" 
    android:text="@string/btnTempExit" 
    android:textSize="14sp" /> 

<Button 
    android:id="@+id/btnExit" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="1dp" 
    android:background="?android:attr/dividerVertical" 
    android:text="@string/btnExit" 
    android:textSize="14sp" /> 

此家庭活动和常用导航抽屉菜单正常显示,但家庭活动中的组件不工作,但抽屉组件正在工作。

回答

1

变化的主要XML成这样子并加载sidemenu到侧菜单框架:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/drawer_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

    <RelativeLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:paddingBottom="@dimen/activity_vertical_margin" 
      android:paddingLeft="@dimen/activity_horizontal_margin" 
      android:paddingRight="@dimen/activity_horizontal_margin" 
      android:paddingTop="@dimen/activity_vertical_margin" 
      tools:context="sas.mobi.lakshmi.main.BaseActivity" > 

     <FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </RelativeLayout> 

    <FrameLayout 
      android:id="@+id/side_menu" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

</android.support.v4.widget.DrawerLayout> 
+0

感谢您的代码。这是工作的家伙。我添加了一些增强功能。它的工作完美。 –

1

喜用碎片为了更好地实现导航画用不能做活动,你需要更换片段它非常简单,所以好对您的菜单上的每一个呼叫,请找到下面的链接http://www.androidhive.info/2015/04/android-getting-started-with-material-design/ 或者您可以通过从一个新的项目选择导航UI创建在Android Studio中新的项目

+0

感谢您的回复。但是我增加了一些增强功能,它可以工作。我只需要知道一些东西,真正的片段很容易使导航抽屉与我的设计相比。这很简单,我想尝试一下。谢谢。 :) –

+0

upvote如果你喜欢我的回答,是的只是尝试一个片段的样本,你可以在一个屏幕上玩很多 – impathuri

+0

确实老兄:)你有任何链接去参考? –

1

由于mencioned here你需要用ID content_frame INSI移动你的FrameLayout de DrawerLayout类似的东西

<RelativeLayout xmlns:tools="http://schemas.android.com/tools" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="sas.mobi.lakshmi.main.BaseActivity" > 



<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
    <!-- The main content view --> 
    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <!-- The navigation drawer --> 
    <ListView 
     android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#fff" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" /> 
</android.support.v4.widget.DrawerLayout>