【问题标题】:Custom ActionBar Listview selected color自定义 ActionBar Listview 选定颜色
【发布时间】:2017-04-18 14:07:24
【问题描述】:

我正在使用自定义操作栏,因此我使用我的代码在 @drawable/menu/themes.xml 中创建了一个名为themes 的 xml 文件:

  <?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
     parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
<item name="android:windowNoTitle">false</item>
<item name="android:windowFullscreen">true</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
     parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#2d73c4</item>
</style>

</resources>

我还使用我的代码创建了一个 .xml 文件名操作栏,并将其放入 @drawable/menu/actionbar.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@+id/search"
    android:showAsAction="always"
    android:title="Αναζήτηση"
    android:icon="@drawable/ic_action_search"/>

</menu>

所以我希望当我使用列表视图时,在我选择的项目上更改颜色。 我在我的主题.xml 中尝试了这一行,但没有成功。

 <item name="android:colorActivatedHighlight">@android:color/transparent</item>

【问题讨论】:

  • 不能安静地理解你的问题,你有一个列表视图,当你选择一个项目时,你的操作栏应该改变它的颜色?哪种颜色?背景?
  • 完全正确。直到现在当我选择项目时不会改变背景颜色。
  • 您是否在应用中添加了Toolbar 小部件?你可以通过 id 找到它,并在后面的代码中设置它的背景颜色。

标签: java android xamarin


【解决方案1】:

我假设您正在为您的操作栏使用Toolbarandroid.support.v7.widget.Toolbar 小部件,那么我在这里举一个使用Toolbar 小部件的示例:

<?xml version="1.0" encoding="utf-8" ?>
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/actionBarSize"
    android:background="?android:attr/colorPrimary"
    android:theme="@android:style/ThemeOverlay.Material.Dark.ActionBar" />

然后在您包含ListView 用于更改操作栏背景颜色的页面中,您可以包含此Toolbar

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar" />

  <ListView
    android:id="@+id/lv"
    android:layout_height="match_parent"
    android:layout_width="match_parent" />

</LinearLayout>

然后在后面的代码中通过它的 id 找到这个 Toolbar 并订阅 listview 的 ItemSelected 事件:

public Toolbar toolbar;

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    SetContentView(Resource.Layout.Main);

    toolbar = FindViewById<Toolbar>(Resource.Id.toolbar);
    SetActionBar(toolbar);

    ListView lv = (ListView)FindViewById(Resource.Id.lv);

    lv.ItemSelected += Lv_ItemSelected;
}

最后设置背景颜色例如这样:

private void Lv_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{

   toolbar.SetBackgroundColor(Color.BlueViolet);
}

当然你可以在AdapterView.ItemSelectedEventArgs找到物品的信息,并根据参数来设置颜色。

【讨论】:

  • 我没有使用 android.support.v7.widget.Toolbar。不幸的是它不起作用。但我也在 MyListViewAdapter 中尝试过它并且它有效。 row.click+=delegate{.SetBackgroundColor(Color.BlueViolet);};但是它在项目点击事件中不起作用我的主要活动代码
  • @Dim,Toolbarandroid.support.v7.widget.Toolbar 都可以工作,我的示例使用 Toolbar。我不知道为什么你的代码不起作用,它在我身边很好,我使用的事件是ItemSelected ,也许你可以分享你的示例,或者你的主要活动的更多代码,包括xml代码和后端代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-19
  • 2018-07-20
  • 2015-06-04
  • 1970-01-01
相关资源
最近更新 更多