Android:CardView按钮无法设置背景颜色

时间:2016-01-09 02:07:34

标签: android button material-design android-cardview

我正在使用本教程 http://code.tutsplus.com/tutorials/creating-a-login-screen-using-textinputlayout--cms-24168

尝试使用CardView和TextView创建一个凸起的按钮。它有点有用,但是存在问题。

enter image description here

目标是使整个按钮具有青色,这是我的colorPrimary。但是你只能看到我的TextView有那种颜色,因为我手动设置它。

这是我的布局:

 <android.support.v7.widget.CardView
                    android:id="@+id/card_view"
                    android:layout_width="wrap_content"
                    android:layout_height="36dp"
                    android:layout_marginTop="16dp"
                    android:clickable="true"
                    android:foreground="?android:attr/selectableItemBackground"
                    card_view:cardBackgroundColor="@color/colorPrimary"
                    android:background="@color/colorPrimary"
                    card_view:cardCornerRadius="2dp"
                    card_view:cardElevation="1dp">

                    <RelativeLayout
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent">

                        <TextView
                            android:id="@+id/textView"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_centerInParent="true"
                            android:gravity="center"
                            android:singleLine="true"
                            android:text="@string/login"
                            android:background="@color/colorPrimary"
                            android:textColor="@color/colorTextIcons"/>
                    </RelativeLayout>
                </android.support.v7.widget.CardView>

我不确定为什么这些线路无效:

card_view:cardBackgroundColor="@color/colorPrimary"
                    android:background="@color/colorPrimary"

2 个答案:

答案 0 :(得分:1)

尝试将背景属性放入RelativeLayout标记而不是CardView标记:

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    static HDEVNOTIFY hDeviceNotify = 0;
    if (msg == WM_CREATE)
    {
        DEV_BROADCAST_DEVICEINTERFACE filter = { sizeof(DEV_BROADCAST_DEVICEINTERFACE) };
        filter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
        filter.dbcc_classguid = GUID_DEVINTERFACE_VOLUME;
        hDeviceNotify = RegisterDeviceNotification(hWnd, &filter,
                DEVICE_NOTIFY_WINDOW_HANDLE);
        return 0;
    }

    if (msg == WM_DEVICECHANGE && wParam == DBT_DEVICEARRIVAL)
    {
        OutputDebugString(L"DBT_DEVICEARRIVAL\n");
        PDEV_BROADCAST_HDR header = (PDEV_BROADCAST_HDR)lParam;
        if (header && (header->dbch_devicetype == DBT_DEVTYP_VOLUME))
        {
            PDEV_BROADCAST_VOLUME volume = (PDEV_BROADCAST_VOLUME)lParam;
            if (volume)
            {
                wchar_t buf[100];
                swprintf_s(buf, 100, L"drive %d %c: ", volume->dbcv_unitmask,
                    FirstDriveFromMask(volume->dbcv_unitmask));
                OutputDebugString(buf);
            }
        }
    }

    //... UnregisterDeviceNotification
}

因此,更正后的代码应为:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/colorPrimary" >

您可能还想移动前景&#39;相应的属性

答案 1 :(得分:-1)

您必须使用getOut()属性更改按钮的颜色,而不是更改按钮的colorButtonNormal属性才能使其正常工作。 您可以使用以下样式执行此操作:

background

在styles.xml中

<Button  
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
    android:theme="@style/YourColorButton"/>

确保您使用的是最新版本的<style name="YourColorButton" parent="Theme.AppCompat.Light"> <item name="colorButtonNormal">@color/yourButtonColorHere</item> </style> 支持库。