为什么会layout_width =" WRAP_CONTENT"占用屏幕的整个宽度?宽度、屏幕、layout_width、QUOT

2023-09-04 05:32:46 作者:草莓熊

通过这个XML:

<TableRow >
    <Button
        android:id="@+id/buttonNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>

我希望WRAP_CONTENT只有一样宽必要在按钮上显示的文字是,但按钮垄断了屏幕的整个宽度...?

I would expect "wrap_content" to only be as wide as necessary to display the text on the button, but the button monopolizes the entire width of the screen...?

推荐答案

您的问题是,在你的XML宽度规格将被忽略。从文档来的TableRow 的:

Your problem is that the width specification in your xml is ignored. From the docs for TableRow:

通过tablerow的孩子不需要指定layout_width和layout_height在XML文件中的属性。的TableRow总是强制这些值分别MATCH_PARENT和WRAP_CONTENT。

The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

P.S。你有一个令人信服的理由使用绝对像素?这通常被认为是不好的做法。

P.S. Did you have a compelling reason to use absolute pixels? That's generally considered bad practice.

P.P.S。双张贴我的答案去与双张贴您的问题。 :)

P.P.S. Double-posting my answer to go with the double-posting of your question. :)