Android: CardView Button 无法设置背景颜色
Android: CardView Button can't set background color
我正在使用本教程
http://code.tutsplus.com/tutorials/creating-a-login-screen-using-textinputlayout--cms-24168
尝试使用 CardView 和 TextView 创建一个凸起的按钮。有点效果,但有一个问题。
目标是让整个按钮具有蓝绿色,这是我的 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"
尝试将背景属性放到 RelativeLayout 标签而不是 CardView 标签:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimary" >
所以你修改后的代码应该是:
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_marginTop="16dp"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="1dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimary">
<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:textColor="@color/colorTextIcons"/>
</RelativeLayout>
您可能还想相应地移动 'foreground' 属性
您必须使用 colorButtonNormal
属性更改按钮的颜色,而不是更改按钮的 background
属性才能使其正常工作。
您可以使用这样的样式来执行此操作:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:theme="@style/YourColorButton"/>
在你的styles.xml
<style name="YourColorButton" parent="Theme.AppCompat.Light">
<item name="colorButtonNormal">@color/yourButtonColorHere</item>
</style>
确保您使用的是最新版本的 appcompat
支持库。
我正在使用本教程 http://code.tutsplus.com/tutorials/creating-a-login-screen-using-textinputlayout--cms-24168
尝试使用 CardView 和 TextView 创建一个凸起的按钮。有点效果,但有一个问题。
目标是让整个按钮具有蓝绿色,这是我的 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"
尝试将背景属性放到 RelativeLayout 标签而不是 CardView 标签:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimary" >
所以你修改后的代码应该是:
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_marginTop="16dp"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="1dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimary">
<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:textColor="@color/colorTextIcons"/>
</RelativeLayout>
您可能还想相应地移动 'foreground' 属性
您必须使用 colorButtonNormal
属性更改按钮的颜色,而不是更改按钮的 background
属性才能使其正常工作。
您可以使用这样的样式来执行此操作:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:theme="@style/YourColorButton"/>
在你的styles.xml
<style name="YourColorButton" parent="Theme.AppCompat.Light">
<item name="colorButtonNormal">@color/yourButtonColorHere</item>
</style>
确保您使用的是最新版本的 appcompat
支持库。