Android 抽屉选择器 state_activated 不工作并且不为默认选择的项目着色
Android drawer selector state_activated doesn't work and doesn't coloring defeault selected item
我正在与这个可怕的错误作斗争。似乎一切正常而不是这个 android:state_activated
。我试图以不同的顺序放置 <item>
但它没有帮助。此外,当应用程序启动时,抽屉中默认选择的片段选项也没有着色(因为它在默认版本中)...
这是我的 selector
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPirmaryDarkerGray"
android:state_activated="true"/>
<item android:drawable="@color/colorPirmaryDarkerGray"
android:state_pressed="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>
这是一项的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="@+id/itemIcon"/>
<TextView
android:id="@android:id/text1"
android:duplicateParentState="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
/>
</LinearLayout>
这是抽屉布局:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:paddingTop="10dp"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#cccc"
android:drawSelectorOnTop="false"
android:listSelector="@drawable/my_drawer_selector"
tools:context="com.myPackage.app.NavigationDrawerFragment"/>
这个在onCreateView
(我这里只加了自己的适配器,其他都是默认的)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.drawer_dashboard, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
String[] items = {
getString(R.string.Contacts),
getString(R.string.Data)
};
Integer[] images = {
R.drawable.ic_contacts,
R.drawable.ic_data
};
mDrawerListView.setAdapter(new MyCustomDrawerListAdapter(getActivity(), items, images));
//This should indicate the first coloring but it doesn't
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
最后,我的 MyCustomDrawerListAdapter
:
private class MyCustomDrawerListAdapter extends ArrayAdapter<String>{
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
public MyCustomDrawerListAdapter(Activity context, String[] itemname, Integer[] imgid){
super(context, R.layout.my_menu_drawer_item, itemname);
this.context = context;
this.itemname = itemname;
this.imgid = imgid;
for (Integer anImgid : imgid) {
System.out.println(" image id " + anImgid);
}
}
public View getView(int position,View view,ViewGroup parent) {
if (view == null) {
view = getActivity().getLayoutInflater().inflate(R.layout.my_menu_drawer_item, null, false);
}
((TextView) view.findViewById(android.R.id.text1))
.setText(getItem(position));
((ImageView) view.findViewById(R.id.itemIcon))
.setImageResource(imgid[position]);
return view;
}
}
您需要在 res 文件夹中创建一个名为 color 的新包,在 color 包中创建您的 my_drawer_selector 文件
然后将此文件与
一起使用
"@color/my_drawer_selector"
这需要像这样:
nav_item_state_list 是你的 my_drawer_selector
我在 api 21 台设备上 运行ning 时遇到了一些问题。
我解决问题的方法是:
在 ListView
中将高亮颜色设置为背景(在我的例子中是 colorAccent)。
android:background="@color/colorAccent"
并设置 ListView
select 或:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorAccent"/>
</shape>
对于抽屉项目,将 LinearLayout
顶级容器背景设置为
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent" android:state_selected="true"/>
<item android:drawable="@android:color/transparent" android:state_pressed="true"/>
<item android:drawable="@android:color/transparent" android:state_activated="true"/>
<item android:drawable="@color/colorMenuBackground"/>
</selector>
就我而言:
<color name="colorMenuBackground">#FFFFFF</color>
到select第一项,在onPostCreate() of your main activity 运行:
setItemChecked(0,true);
在列表视图上(0 是项目的位置)
事实上,这就是我使用与 Hamburguer 菜单按钮同步的方法:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
drawerList.setItemChecked(1,true);
}
我正在与这个可怕的错误作斗争。似乎一切正常而不是这个 android:state_activated
。我试图以不同的顺序放置 <item>
但它没有帮助。此外,当应用程序启动时,抽屉中默认选择的片段选项也没有着色(因为它在默认版本中)...
这是我的 selector
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorPirmaryDarkerGray"
android:state_activated="true"/>
<item android:drawable="@color/colorPirmaryDarkerGray"
android:state_pressed="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>
这是一项的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:id="@+id/itemIcon"/>
<TextView
android:id="@android:id/text1"
android:duplicateParentState="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
/>
</LinearLayout>
这是抽屉布局:
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:paddingTop="10dp"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#cccc"
android:drawSelectorOnTop="false"
android:listSelector="@drawable/my_drawer_selector"
tools:context="com.myPackage.app.NavigationDrawerFragment"/>
这个在onCreateView
(我这里只加了自己的适配器,其他都是默认的)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.drawer_dashboard, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
String[] items = {
getString(R.string.Contacts),
getString(R.string.Data)
};
Integer[] images = {
R.drawable.ic_contacts,
R.drawable.ic_data
};
mDrawerListView.setAdapter(new MyCustomDrawerListAdapter(getActivity(), items, images));
//This should indicate the first coloring but it doesn't
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
最后,我的 MyCustomDrawerListAdapter
:
private class MyCustomDrawerListAdapter extends ArrayAdapter<String>{
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
public MyCustomDrawerListAdapter(Activity context, String[] itemname, Integer[] imgid){
super(context, R.layout.my_menu_drawer_item, itemname);
this.context = context;
this.itemname = itemname;
this.imgid = imgid;
for (Integer anImgid : imgid) {
System.out.println(" image id " + anImgid);
}
}
public View getView(int position,View view,ViewGroup parent) {
if (view == null) {
view = getActivity().getLayoutInflater().inflate(R.layout.my_menu_drawer_item, null, false);
}
((TextView) view.findViewById(android.R.id.text1))
.setText(getItem(position));
((ImageView) view.findViewById(R.id.itemIcon))
.setImageResource(imgid[position]);
return view;
}
}
您需要在 res 文件夹中创建一个名为 color 的新包,在 color 包中创建您的 my_drawer_selector 文件
然后将此文件与
一起使用"@color/my_drawer_selector"
这需要像这样:
nav_item_state_list 是你的 my_drawer_selector
我在 api 21 台设备上 运行ning 时遇到了一些问题。
我解决问题的方法是:
在 ListView
中将高亮颜色设置为背景(在我的例子中是 colorAccent)。
android:background="@color/colorAccent"
并设置 ListView
select 或:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/colorAccent"/>
</shape>
对于抽屉项目,将 LinearLayout
顶级容器背景设置为
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/transparent" android:state_selected="true"/>
<item android:drawable="@android:color/transparent" android:state_pressed="true"/>
<item android:drawable="@android:color/transparent" android:state_activated="true"/>
<item android:drawable="@color/colorMenuBackground"/>
</selector>
就我而言:
<color name="colorMenuBackground">#FFFFFF</color>
到select第一项,在onPostCreate() of your main activity 运行:
setItemChecked(0,true);
在列表视图上(0 是项目的位置)
事实上,这就是我使用与 Hamburguer 菜单按钮同步的方法:
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
drawerList.setItemChecked(1,true);
}