Android按钮在通过代码添加时外观不同。

huangapple go评论61阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;

    &lt;LinearLayout
        android:id=&quot;@+id/viewCommands&quot;
        android:orientation=&quot;vertical&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        &gt;
        &lt;Button
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:text=&quot;Button in xml&quot;
            /&gt;

    &lt;/LinearLayout&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

This is the code

Button btn = new Button(this);
btn.setText(&quot;button by code&quot;);
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?
Android按钮在通过代码添加时外观不同。

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);
...

huangapple
  • 本文由 发表于 2023年5月14日 01:08:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定