英文:
Android Buttons look different when added by code
问题
我发现通过代码添加到布局中的按钮与在XML中定义的按钮看起来非常不同。我制作了一个简短的示例。
这是布局部分:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="@+id/viewCommands"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button in xml" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是代码部分:
Button btn = new Button(this);
btn.setText("button by code");
btn.setBackgroundColor(0xff00e033);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout lo = findViewById(R.id.viewCommands);
lo.addView(btn, lp);
这是它的外观。相当不同,不是吗?
我在代码中没有添加按钮的任何样式,除了背景颜色,所以难怪它看起来很无趣。但我在XML中也没有添加任何东西,因为这两个按钮都放在同一个布局中。如果布局会为元素添加样式,那么有什么区别呢?
我希望能更深入地了解这个问题。如何使通过代码添加的按钮看起来与来自XML的按钮相同?
请注意,与我找到的许多其他问题不同,这个问题不涉及不同设备、Android版本、框架、分辨率、应用程序等的不同外观,它发生在完全相同的布局中。
英文:
I found that a button that is added to a layout by code looks very different than a button defined in xml.
I made a short example.
Here is the layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="@+id/viewCommands"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button in xml"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
This is the code
Button btn = new Button(this);
btn.setText("button by code");
btn.setBackgroundColor(0xff00e033);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout lo = findViewById(R.id.viewCommands);
lo.addView(btn, lp);
And this is what it looks like. Quite different, isn't it?
I did not add any style to the button in code except background color, so no wonder it's so boring.
But I also didn't add anything in xml and as both buttons land in the same layout. If the layout adds style to elements, what's the difference?
I would appreciate to get some more insight in this.
How can I make the code-added buttons look the same as those coming from xml?
please see that in contrast to many other questions I found, this question is not about different look on different devices, Android versions, frameworks, resolutions, apps... it's in the very same layout.
答案1
得分: 1
> 我的应用样式似乎继承自Theme.Material3.DayNight.NoActionBar。现在,我必须为按钮指定什么样式?
因此,XML抽象的Button
应自动转换为符合应用主题的适当按钮版本。在以编程方式执行此操作时,情况并非如此,因为确切的Java/Kotlin Button类已经被指定和导入。
所引用的答案适用于Theme.AppCompat(Material 1),其中其Button版本为AppCompatButton
。
从Material components(Material 2库)开始,Button版本是MaterialButton
,它是从AppCompatButton
扩展而来,但具有不同/增强的样式。
由于您使用了Material3
库,因此在以编程方式使用时,需要使用MaterialButton
而不是Button
:
MaterialButton btn = new MaterialButton(this);
...
英文:
> My app style seems to be inherited from Theme.Material3.DayNight.NoActionBar. Now what do I have to specify for my button to use this style?
So, the idea is that the XML abstract Button
is automatically converted to the appropriate button version that comply with the app theme. That is not the case when it comes to do that programmatically because the exact Java/Kotlin Button class is already specified and imported.
The referenced answer is suitable for Theme.AppCompat (Material 1) where its Button version is AppCompatButton
.
Starting from Material components (Material 2 library), the Button version is the MaterialButton
which extends from the AppCompatButton
but has a different/enhanced style.
As you use Material3
library, then you need to use MaterialButton
instead of Button
programmatically:
MaterialButton btn = new MaterialButton(this);
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论