2015-09-30 41 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>