0

我想知道如何更改NavigationDrawer的列表項點擊顏色,如圖中箭頭所示從藍色變爲紅色。 enter image description here更改NavigationDrawer的顏色點擊顏色

我的第二個問題是,我想補充一個標誌(圖像),一旦點擊其中將在動作條(最上面一欄)指示用戶在MainActivity活動,以下是調用ActionBarActivity活動代碼。

public class MainActivity extends ActionBarActivity { 

    private String[] mOptionMenu; 
    private DrawerLayout mDrawerLayout; 
    private RelativeLayout mDrawerRelativeLayout; 
    private ListView mDrawerList; 
    private ActionBarDrawerToggle mDrawerToggle; 

    private CharSequence mTitleSection; 
    private CharSequence mTitleApp; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 



     mOptionMenu = new String[] { "Opción 1", "Opción 2", "Opción 3" }; 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerRelativeLayout = (RelativeLayout) 
     findViewById(R.id.left_drawer); 
     mDrawerList = (ListView) findViewById(R.id.list_view_drawer); 
     mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
     mOptionMenu)); 

     mDrawerList.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
     int position, long id) { 

     Fragment fragment = null; 

     switch (position) { 
     case 0: 
     fragment = new FirstFragment(); 
     break; 
     case 1: 
     fragment = new SecondFragment(); 
     break; 
     case 2: 
     fragment = new ThirdFragment(); 
     break; 
     } 

     FragmentManager fragmentManager = getSupportFragmentManager(); 

     fragmentManager.beginTransaction() 
     .replace(R.id.content_frame, fragment).commit(); 

     mDrawerList.setItemChecked(position, true); 

     mTitleSection = mOptionMenu[position]; 
     getSupportActionBar().setTitle(mTitleSection); 

     mDrawerLayout.closeDrawer(mDrawerRelativeLayout); 
     } 
     }); 
     mDrawerList.setItemChecked(0, true); 
     mTitleSection = getTitle(); 
     mTitleApp = getTitle(); 

     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
     R.drawable.ic_drawer, R.string.drawer_open, 
     R.string.drawer_close) { 

     public void onDrawerClosed(View view) { 
      getSupportActionBar().setTitle(mTitleSection); 
     ActivityCompat.invalidateOptionsMenu(MainActivity.this); 
     } 

     public void onDrawerOpened(View drawerView) { 
      getSupportActionBar().setTitle(mTitleSection); 
     ActivityCompat.invalidateOptionsMenu(MainActivity.this); 
     } 
     }; 

     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     getMenuInflater().inflate(R.menu.main_activity_actions, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     if (mDrawerToggle.onOptionsItemSelected(item)) { 
      return true; 
     } 

     switch (item.getItemId()) { 
     case R.id.action_settings: 
      Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show(); 
      ; 
      break; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 

     return true; 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     mDrawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 
} 

在此先感謝。

+0

使用可繪製XML,你可以做到這一點 – Amy 2014-11-05 06:50:39

+0

抽屜式導航=列表視圖導航抽屜 – krossovochkin 2014-11-05 06:54:24

回答

0

論背景指示器你的第一個問題。它的工作原理上面的API級別11

navigation_list_background.xml添加你想要的顏色

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_activated="true" android:drawable="@color/green" /> 
    <item android:state_selected="true" android:drawable="@color/green" /> 
    <item android:state_pressed="true" android:drawable="@color/light_blue" /> 
    <item android:state_focused="true" android:drawable="@color/light_blue" /> 
    <item android:drawable="@color/blue" /> 

</selector> 

添加到您的基本風格:

<style name="AppTheme" parent="AppBaseTheme"> 
     <item name="android:activatedBackgroundIndicator">@drawable/navigation_list_background</item> 

    </style> 

添加背景,以您的佈局(這是你的項目,你顯示在抽屜裏)

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="?android:attr/activatedBackgroundIndicator" 
     android:orientation="horizontal" > 


     <TextView 
      android:id="@+id/title" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:layout_weight="8" 
      android:text="Option 1" /> 


    </LinearLayout> 

希望這個作品。

並回答第二個問題。您可以使用自定義操作欄。

customactionbar.xml

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/header_drawer" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/drawermenu" /> 

     <TextView 
      android:id="@+id/header_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginLeft="5dp" 
      android:text="TEST" 
      android:textStyle="bold" /> 
     <ImageView 
      android:id="@+id/button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/button" /> 
    </LinearLayout> 

    <View 
     android:id="@+id/actionbarseperator" 
     android:layout_width="match_parent" 
     android:layout_height="3dp" 
     android:background="@color/blue" /> 

</LinearLayout> 

Yourlayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <include layout="@layout/actionbar" /> 


    <--Your Layout--> 
</LinearLayout> 

在Java添加此setcontent

this.requestWindowFeature(Window.FEATURE_NO_TITLE); 

之前添加的onclick上的按鈕。

Button button=(Button)findViewById(R.id.button); 
    button.setOnClickListener(this); 

    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch(v.getId()) 
    { 
    case R.id.button: 
    ActivityYouwantToOpen.onOpen(v); 
     break; 
     } 
     } 

它長的帖子:P!

1

您可以使用選擇器更改mDrawerList中按下狀態的顏色。首先,在資源在你的資源創建一個紅色固體繪製形狀(說redbg)這樣的/可繪製/ redbg.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="@android:color/holo_red_dark"/> 
</shape> 

然後創建一個選擇說(listviewbg)/繪製/ listviewbg.xml像這

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@drawable/redbg"/> 
</selector> 

,並應用此BG到你的mDrawerList在XML

<ListView 
android:id="@+id/mDrawerList " 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:listSelector="@drawable/listviewbg" 
/> 
+0

和內部遺憾沒有得到你的第二個問題:(PLZ解釋了一下。 – 2014-11-05 07:50:55