Android “如何修复不工作的问题”;安卓:背景“;在安卓工作室?
当我将Android “如何修复不工作的问题”;安卓:背景“;在安卓工作室?,android,xml,Android,Xml,当我将android:background与按钮一起使用时,该按钮的外观不会改变,但当我将其与TextView或ImageButton一起使用时,它会正常工作。该问题出现在项目中的所有布局文件中 如何解决这个问题 activity.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background
与按钮一起使用时,该按钮的外观不会改变,但当我将其与TextView
或ImageButton一起使用时,它会正常工作。该问题出现在项目中的所有布局文件中
如何解决这个问题
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
//Not working
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button"/>
/Working
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:layout_marginLeft="100dp"/>
</RelativeLayout>
//不起作用
/工作
AndroidManifest.xml
android:largeHeap=“true”
android:supportsRtl=“true”
android:theme=“@style/theme.MaterialComponents.Light.NoActionBar”>
这是在按钮中使用后台的代码,上面提供的是清单xml,请提供您的活动xml
我知道我来晚了,但我也遇到了这个问题。我无法在XML中设置按钮的字体或背景。我发现简单地创建您自己的Button类(从Button扩展而来)就可以解决这个问题
虽然不是最理想的解决方案,但它很有效,也很简单
爪哇:
XML:
android:background
在我的项目中不使用按钮。但是使用ImageButton
和TextView
。我已经使用了这段代码,请检查它的工作情况,好像还有其他错误
this is the code to use background in button and kindly the above provided is manifest xml provide your activity xml
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="testing"
android:textColor="#ffffff"
android:background="@drawable/ic_launcher_background"/>
public class MyButton extends androidx.appcompat.widget.AppCompatButton {
public MyButton(Context context) {
super(context);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
<com.example.yourpackage.MyButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/mybackground"/>