[英]Custom Button not displaying inside Layout (RelativeLayout and LinearLayout)
我為自定義按鈕創建了三個可繪制對象,如下面的代碼片段所示
custom_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/button_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/button_default" />
</selector>
button_default.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#fff" />
<stroke
android:color="#f71b5d"
android:width="0.5dp" />
<corners
android:bottomLeftRadius="5dp"
android:topRightRadius="5dp" />
<padding
android:bottom="0dp"
android:left="0dp"
android:right="0dp"
android:top="0dp" />
</shape>
button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ebc2ce" />
<stroke
android:color="#f71b5d"
android:width="0.5dp" />
<corners
android:bottomLeftRadius="5dp"
android:topRightRadius="5dp" />
<padding
android:bottom="7dp"
android:left="7dp"
android:right="7dp"
android:top="7dp" />
</shape>
這是我嘗試使用這些可繪制對象創建自定義按鈕的父布局。 自定義按鈕在LinearLayout內部
fancy_card.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_gravity="center"
android:clickable="true"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/fancy_card_bg">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/custom_button"/>
</LinearLayout>
</android.support.v7.widget.CardView>
我的問題 : LinearLayout
中的按鈕沒有顯示,但是當我從按鈕的背景( android:background="@drawable/custom_button"
)中刪除自定義可android:background="@drawable/custom_button"
,現在可以看到該按鈕。 如果我從LinearLayout中刪除按鈕,然后將我的custom_button
可繪制背景應用到該按鈕,則它將起作用。 我真的對這里發生的事情感到困惑。
給Button指定一個ID,並在Java文件中定義按鈕背景。
btn.setBackgroundResource(R.drawable.custom_button);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.