英文:
What are these bunch of non-existence methods in material button google reference?
问题
我尝试以编程方式更改材料按钮图标,为此,Stackoverflow上的帖子建议使用名为setCompoundDrawablesWithIntrinsicBounds
的易碎方法。
我试图在官方网站上查找更多方法,例如developer.android.com和GitHub,但奇怪的是,我找到的方法和XML属性根本不存在。例如,此页面建议以下内容:
据我所知,XML中不存在app:MaterialButton_icon
或android:MaterialButton_icon
属性。而且在Java 8中也没有setIconResource(int)
方法。
这些奇怪的参考文档是什么?
英文:
I tried to change material button icon programmatically and for this, Stackoverflow posts suggest that there is a frangible method called setCompoundDrawablesWithIntrinsicBounds
.
I tried to find more methods on the official sites e.g. developer.android.com and GitHub, But oddly I find methods and XML-attributes that do not exists at all. For example this page suggests the following:
As I know, There is no app:MaterialButton_icon
nor android:MaterialButton_icon
attributes for XML. Also there is no setIconResource(int)
method (in Java 8).
What are these odd reference documents?
答案1
得分: 2
在官方网站上看到的名称只是用于该视图的自定义可样式化属性名称。
您可以在此链接中了解更多信息:https://developer.android.com/develop/ui/views/layout/custom-views/create-view#customattr
在XML中,您只需简单地使用 app:icon="@drawable/icon"
,而不是例如 app:MaterialButton_icon="@drawable/icon"
。
如果您在代码中找不到这些方法,请检查您的项目依赖项,确保它具有Google Material依赖项。或者如果您在使用Kotlin,像 setIcon
或 getIcon
这样的方法只是 icon
属性,没有任何函数。
希望这可以解决您的问题。
英文:
The names that you see on the official site are just custom styleable attribute names which is used for that View.
You can learn more about it in this link: https://developer.android.com/develop/ui/views/layout/custom-views/create-view#customattr
In XML, you can just simply use app:icon="@drawable/icon"
instead of app:MaterialButton_icon="@drawable/icon"
for example.
If you cannot find those methods in the code, please check your project dependencies to make sure it has Google Material dependency. Or if you are using Kotlin, the method like setIcon
or getIcon
is simply icon
property without any functions.
Hope this can resolve your problems
答案2
得分: 1
我不知道那个突出显示的 MaterialButtom_icon
属性是什么,但似乎您在您的Java代码中使用了 Button
而不是 MaterialButton
。材料按钮的正确声明如下:
MaterialButton button = findViewById(R.id.your_material_button_id);
而不是
Button button = findViewById(R.id.your_material_button_id);
通过正确的声明,我们有:
英文:
I don't know what is that highlighted MaterialButtom_icon
attribute but it seems that you are using Button
instead of MaterialButton
in your java code. The correct declaration of material button is as:
MaterialButton button = findViewById(R.id.your_material_button_id);
And Not
Button button = findViewById(R.id.your_material_button_id);
By the correct declaration then we have:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论