【问题标题】:How to change text color of a custom view list item for menu drawer when selected - DrawerLayout选择时如何更改菜单抽屉的自定义视图列表项的文本颜色 - DrawerLayout
【发布时间】:2014-07-28 15:13:05
【问题描述】:

我是一名新的安卓开发者。在我的应用程序中,我有一个使用 android 的 DrawerLayout 的抽屉:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="@drawable/menu"/>
</android.support.v4.widget.DrawerLayout>

这是我对列表项的自定义布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/itemLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:orientation="vertical"
        android:layout_marginTop="0dp"
        android:background="?android:attr/activatedBackgroundIndicator">

        <LinearLayout

            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/drawer_icon"
                android:layout_width="45dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="4dp"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:layout_centerVertical="true"
                android:padding="4dp"/>

            <TextView
                android:id="@+id/drawer_itemName"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:minHeight="40dp"
                android:textAppearance="@android:style/TextAppearance.Medium"
                android:gravity="center_vertical"
                android:paddingRight="40dp"
                android:textColor="@android:color/white"/>

            <TextView
                android:id="@+id/drawer_itemExtra"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#ffef5c6f"
                android:layout_gravity="center"
                android:background="@drawable/menu_counter"
                android:layout_marginLeft="20dp"/>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_marginBottom="1dp"
            android:layout_marginTop="1dp"
            android:layout_marginLeft="70dp"
            android:background="@android:color/white"
            android:alpha="0.4"
            ></View>

    </LinearLayout>

</RelativeLayout>

现在我想在选中时更改第一个 TextView 的颜色,并摆脱我知道来自此的当前选定背景颜色:

android:background="?android:attr/activatedBackgroundIndicator"

有人可以帮忙吗?

【问题讨论】:

  • This Question/Answer 应该有帮助
  • 它关于背景我需要改变文字颜色
  • 您找到解决方案了吗?
  • 是的,你需要为不同的状态使用选择器并将其用作TextView背景

标签: android xml slidingdrawer drawerlayout


【解决方案1】:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#FFFFFF" /> <!-- pressed -->
<item android:state_focused="true" android:color="#ef5c6f" /> <!-- focused -->
<item android:state_activated="true" android:color="#FFFFFF" /> <!-- activated -->
<item android:color="#ef5c6f" /> <!-- default -->
</selector>

【讨论】:

    猜你喜欢
    • 2015-07-03
    • 2015-11-09
    • 2016-06-16
    • 2015-09-02
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2014-01-21
    相关资源
    最近更新 更多