以编程方式创建 Android 布局时与在 XML 中不同 width/weight

Different width/weight when creating Android layout programmatically vs. in XML

我想创建具有相同宽度并拉伸到(匹配)父项的按钮。我需要以编程方式 来完成它。首先,我创建了一个所需状态的布局草稿(仅关注按钮的绿线):

<LinearLayout
        android:id="@+id/greenLine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="@dimen/button_bottom_padding">

        <!-- This vertical LinearLayout with Button repeats 5x (Each Button has different text and ID) -->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:gravity="center|center_horizontal"
            android:paddingLeft="@dimen/button_left_padding"
            android:paddingRight="@dimen/button_right_padding">

            <Button
                android:id="@+id/button"
                android:text="O"
                style="@style/green_button" />
        </LinearLayout>
        <!-- 4 more time -->

    </LinearLayout>

然后我把草稿分解成LinearLayout和分开绿色Button。在主布局中只有 horizo​​ntal greenLine LinearLayout:

main.xml

<LinearLayout
        android:id="@+id/greenLine"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="@dimen/button_bottom_padding">

    </LinearLayout>

linear_layout_vertical.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center|center_horizontal"
android:paddingLeft="@dimen/button_left_padding"
android:paddingRight="@dimen/button_right_padding"></LinearLayout>

green_button.xml

<Button xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/green_button" />

绿色按钮样式:

<style name="button">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:gravity">center</item>
    <item name="android:shadowColor">@color/black</item>
    <item name="android:shadowDx">0</item>
    <item name="android:shadowDy">0</item>
    <item name="android:shadowRadius">0</item>
</style>

<style name="green_button" parent="button">
    <item name="android:textColor">@drawable/green_button_state</item>
    <item name="android:background">@drawable/green_button</item>
</style>

之后我得到这个:

这些是我用来创建绿线按钮的函数:

private void _createGreenLine() {

    // Possible values for green line
    String[] values = {"O", "N", "S", "M", "C"};

    for (String value: values) {
        // Layout container
        LinearLayout linearLayout = this._createLinearLayout();
        // Button
        Button button = (Button) this.getLayoutInflater()
                .inflate(R.layout.green_button, null);
        button.setText(value);
        // Button to layout
        linearLayout.addView(button);

        // Layout to green line
        this.greenLine.addView(linearLayout);
    }
}

private LinearLayout _createLinearLayout() {

    return (LinearLayout) this.getLayoutInflater()
            .inflate(R.layout.linear_layout_vertical, null);
}

我也尝试更改添加新的View(我先添加linearLayout,在greenLine上调用getChildAt,最后添加Button)。

非常感谢。

解决方案:为正确显示布局所做的更改

private LinearLayout _createLinearLayout(ViewGroup root) {

    LinearLayout linearLayout = (LinearLayout) this.getLayoutInflater()
            .inflate(R.layout.linear_layout_vertical, root, false);
    return linearLayout;
}

Button button = (Button) this.getLayoutInflater()
                .inflate(R.layout.green_button, (ViewGroup) linearLayout, false);

阅读更多:<Layout Inflation as Intended>

您应该使用 inflate() 方法的第二个参数而不是调用 parent.addView(child)