我想知道如何更改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 :(得分:1)
您可以使用选择器更改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中创建一个选择器说(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"
/>
答案 1 :(得分: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>
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>
在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