【问题标题】:Change color of NavigationDrawer click color [duplicate]更改导航抽屉单击颜色的颜色[重复]
【发布时间】:2014-11-05 06:44:01
【问题描述】:

我想知道如何将 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);
    }
}

提前致谢。

【问题讨论】:

  • 使用 Drawable XML 你可以做到这一点
  • 导航抽屉!= 导航抽屉内的列表视图

标签: java android android-layout android-activity android-fragments


【解决方案1】:

关于背景指示器的第一个问题。它在 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>

你的布局.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>

在 setcontent 之前将其添加到您的 Java 中

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

【讨论】:

    【解决方案2】:

    您可以使用选择器更改 mDrawerList 中按下状态的颜色。首先,在 res/drawable/redbg.xml 中创建一个像这样的红色纯色可绘制形状(比如 redbg)

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

    然后像这样在 res/drawable/listviewbg.xml 中创建一个选择器 say(listviewbg)

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

    并将这个 bg 应用到 xml 中的 mDrawerList

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

    【讨论】:

    • 很抱歉没有得到你的第二个问题:(。请解释一下
    猜你喜欢
    • 1970-01-01
    • 2016-06-16
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-02
    • 2015-11-09
    相关资源
    最近更新 更多