Rya*_*one 40 android android-listview android-navigation
我使用Android Studio来实现导航抽屉,我无法获得用于显示我们当前要更改的部分的蓝色.
我尝试了很多东西,我目前正在使用listSelector,它看起来像:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/selected" />
<item android:state_pressed="true" android:drawable="@color/highlight" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我也试过了state_checked
.state_pressed
在这种情况下工作,但当前选择的项目仍然是蓝色.
编辑:我一直在研究这个,当创建适配器时,传递的上下文是getActionBar().getThemedContext()
如此我想如果我能找到正确的属性来分配我的操作栏样式我可以从那里改变它.我尝试了几个不同的属性而没有运气.有谁知道确切的属性?
我也意识到我是否愿意
<item name="android:activatedBackgroundIndicator">@drawable/nav_listview_selector</item>
在我的主题和变化的主要部分getActionBar().getThemedContext()
的getActivity.getBaseContext
话,我可以改变颜色,但我不认为这是正确的方式.我认为应该使用主题背景.因此,如果有人知道activatedBackgroundIndicator
可以放在哪里,那么它将被用于getActionBar.getThemedContext()
EDIT2:
因此,用于listview的文本视图是SDK中的一个,如下所示:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
Run Code Online (Sandbox Code Playgroud)
所以我尝试修改"?android:attr/activatedBackgroundIndicator"
主题级别,但它对选中/选中/激活没有效果,但它对按下有效.有人知道为什么吗?我怎么能改变它?
Ash*_*awy 52
要解决这个问题:
1-你的ListView下不需要android:listSelector.
2- 在(res/values)下打开(或创建)styles.xml.
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:activatedBackgroundIndicator">@drawable/drawer_list_selector</item>
</style>
Run Code Online (Sandbox Code Playgroud)
3-在res/drawable文件夹下创建drawer_list_selector.xml文件
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/light_gray_color" />
<item android:state_activated="true" android:drawable="@drawable/red_color" />
<item android:drawable="@android:color/transparent" />
</selector>
Run Code Online (Sandbox Code Playgroud)
4-在res/drawable下创建red_color.xml/light_gray_color.xml(或任何其他名称)并添加所需的Hex颜色:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#C8FF0000"/>
</shape>
Run Code Online (Sandbox Code Playgroud)
5-打开你的项目AndroidManifest.xml并添加android:theme标签(如果不存在)
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
Run Code Online (Sandbox Code Playgroud)
参考/信用:更改导航抽屉所选项目颜色从默认蓝色
小智 9
要更改" 所选项目的导航抽屉项目背景颜色 ",您可以使用attribut执行此操作:
colorControlHighlight
在你的" styles.xml "中,它看起来像这样:
<style name="YourStyleNameFor.NavigationDrawer" parent="ThemeOverlay.AppCompat.Light">
<item name="colorControlHighlight">@color/your_highlight_color</item>
</style>
Run Code Online (Sandbox Code Playgroud)
不要忘记 在标签中应用您的Style:
android.support.design.widget.NavigationView
例如,在您的activity_main.xml中,它可能如下所示:
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/navigationdrawer_header"
<!-- the following line referes to your style -->
app:theme="@style/YourThemeNameFor.NavigationDrawer"
<!-- the following two lines are maybe also interesting for you -->
app:itemIconTint="@color/gray3"
app:itemTextColor="@color/gray3"
app:menu="@menu/navigationdrawer_main" />
Run Code Online (Sandbox Code Playgroud)
这里是如何我已经做了,它是工作,短暂的概念是保持位置所选项目的适配器,并呼吁notifyDataSetChanged
在调用notifyDatasetChanged
该getView
方法再次被调用,并在获取视图查看所选位置的位置改变的背景视图.这是代码:
NavigationDrawer列表视图的适配器
public class MenuAdapter extends BaseAdapter {
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private Context mContext;
private String name;
private int profile;
private int mIcons[];
private int selectedPosition = 0;
private String mNavTitles[];
private LayoutInflater mInflater;
public MenuAdapter(String titles[], int icon[], String Name, int profile) {
mNavTitles = titles;
mIcons = icon;
name = Name;
this.profile = profile;
}
public MenuAdapter(String Titles[], int Icons[], Context mContext) {
mNavTitles = Titles;
mIcons = Icons;
this.mContext = mContext;
mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mNavTitles.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(R.layout.item_row, parent, false);
TextView textView = (TextView) convertView.findViewById(R.id.rowText);
ImageView imageView = (ImageView) convertView.findViewById(R.id.rowIcon);
final LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.outerLayout);
imageView.setImageResource(mIcons[position]);
textView.setText(mNavTitles[position]);
if (position == selectedPosition)
layout.setBackgroundColor(mContext.getResources().getColor(R.color.app_bg));
else
layout.setBackgroundColor(mContext.getResources().getColor(R.color.normal_bg));
return convertView;
}
public void setSelectedPosition(int position) {
this.selectedPosition = position;
}
}
Run Code Online (Sandbox Code Playgroud)
在你的活动中这样做
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
mMenuAdapter.setSelectedPosition(position - 1);
mMenuAdapter.notifyDataSetChanged();
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人仍然遇到任何困难,请随时问.
我用自定义适配器类实现抽屉菜单.可能它会帮助某人抽屉列表
<ListView
android:id="@+id/listview_drawer"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/menu_item_color"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:fadingEdge="none"
/>
Run Code Online (Sandbox Code Playgroud)
drawer_list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/menu_selector"
>
<ImageView
android:id="@+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:src="@drawable/ic_menu_home" />
<TextView
android:id="@+id/lblName"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:layout_toRightOf="@+id/imgIcon"
android:minHeight="48dp"
android:textColor="@color/menu_txt_color" />
Run Code Online (Sandbox Code Playgroud)
menu_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<!-- selected -->
<item android:drawable="@color/menu_item_active_color" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@color/menu_item_active_color" android:state_pressed="true"/>
<item android:drawable="@color/menu_item_active_color" android:state_activated="true"/>
<item android:drawable="@color/menu_item_active_color" android:state_checked="true"/>
<item android:drawable="@color/menu_item_active_color" android:state_selected="true"/>
<item android:drawable="@color/menu_item_color" android:state_activated="false"/>
Run Code Online (Sandbox Code Playgroud)
在列表视图的项目点击列表器中添加此项你的视图 yourlistview.setItemChecked(position,true);
小智 5
这为我工作:
首先定义一个drawable item_bg.xml
为:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/nav_menu_bg" />
</selector>
Run Code Online (Sandbox Code Playgroud)然后使用以下可绘制对象navigation_main_layout.xml
:
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemBackground="@drawable/item_bg"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_navigation"
app:menu="@menu/navigation_main_layout_drawer" />
Run Code Online (Sandbox Code Playgroud)仍然不确定为什么它不起作用。但我发现它的方法是使用我自己的simple_list_item_activated
布局传递给 ArrayAdapter,除了将文本颜色设置为白色之外,它基本上是相同的。然后我替换getActionBar().getThemedContext()
为getActivity().getBaseContext()
,现在已经有效果了。
这可能不是正确的方法,并且可能会对将来产生影响,但现在我让它按照我想要的方式工作。
归档时间: |
|
查看次数: |
71968 次 |
最近记录: |