android - 以编程方式与在 XML 中创建 Android 布局时不同的宽度/重量

标签 android android-layout

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

enter image description here

<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>

然后我把草稿分解成verticalLinearLayout和单独的绿色Button。在主布局中只有水平绿线 LinearLayout:

ma​​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">

    </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>

之后我得到这个:

enter image description here

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

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)

关于android - 以编程方式与在 XML 中创建 Android 布局时不同的宽度/重量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27918589/

相关文章:

android - 修改了 URL 的 Firebase 崩溃日志

java - 膨胀 View 时出错

android - 调整大小后,TextView 无法正确换行文本

android - 谷歌地图覆盖图像不可见

android - 从android中的短信查询联系人姓名

android - 如何在相对布局中垂直和水平居中线性布局

android - 为什么 TextView 与 ScrollView 相交?

android - 如何在运行时更改 RadioGroup 的背景颜色?

android - 如何在Android中添加 'remember me'选项?

android - 最初展开的 BottomSheetDialog 不起作用