如何以编程方式制作相同宽度的按钮

时间:2015-09-22 22:30:15

标签: android

我有2个按钮,我需要使它们具有相同(最大)的宽度。 像

这样的东西
LinearLayout.LayoutParams = new LinearLayout.LayoutParams(button1.getWidth(),
                                                          button2.getHeight());
// Or ViewGroup.LayoutParams

但是当我尝试使用这种方式时会产生奇怪的结果,按钮高度也会改变,宽度也不一样。

当我使用LayoutParams时,它也不起作用

project.Suppliers = project.ProjectSuppliers
    .Select(proj => (int?)proj.ProjectSupplier)
    .ToArray();

3 个答案:

答案 0 :(得分:1)

将它们包裹在LinearLayout中,并将width设置为0dp,将weight设置为与1相等的值。我想你也可以通过编程方式做到这一点。

答案 1 :(得分:1)

将两个按钮放在LinearLayout中并设置按钮android:layout_width =" 0dp"和android:layout_weight =" 1"

<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button 1" />
   <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button 2" />
</LinearLayout>

答案 2 :(得分:0)

使用

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:strip-space elements="*"/>

    <xsl:template match="node()">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*">
        <xsl:attribute name="{local-name()}" namespace="{namespace-uri()}">
            <xsl:value-of select="normalize-space(.)" />
        </xsl:attribute>
    </xsl:template>

    <xsl:template match="text()">
        <xsl:value-of select="normalize-space(.)" />
    </xsl:template>
</xsl:template>

而不是

Button.getLayoutParams().width
Button.getLayoutParams().height

获取Button的宽度高度。

并将button1的宽度设置为button2

Button.getWidth()
Button.getHeight()

反之亦然。