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