java - 以编程方式添加带有权重的 EditText

标签 java android android-layout android-layout-weight

我创建了一个垂直 LinearLayout,其中包含一个水平 LinearLayout,以便在其中容纳 3 个 EditText 框以进行数据输入。我还在垂直 LinearLayout 下方放置了一个按钮,我想对其进行编程,以便在每次按下时添加另一行 editText。

到目前为止,我已经成功并设法以编程方式添加另一行,其中包含 1 个编辑文本。然后我尝试了 3,但遇到了障碍。我尝试使用权重和权重将 editText 框分成 3 列。然而,通过下面的代码,我得到了两个彼此相邻的 editText 框,然后是一个非常非常小的编辑框作为最后一个。

我做错了什么?为什么编辑文本框的宽度不除以 3?

XML:

        <LinearLayout android:id="@+id/layout_ingredients"
            android:layout_below="@id/text_recipeIngredients"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="3"
                android:orientation="horizontal">

                <EditText android:id="@+id/edit_recipeIngredient"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:hint="@string/edit_recipe_ingredient"/>

                <EditText android:id="@+id/edit_recipeAmount"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:hint="@string/edit_recipe_amount"/>

                <EditText android:id="@+id/edit_recipeUnit"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:hint="@string/edit_recipe_unit"/>

            </LinearLayout>

        </LinearLayout>

        <Button android:id="@+id/btn_add_ingredient"
            android:background="@color/orange"
            android:layout_marginTop="10dp"
            android:layout_below="@id/layout_ingredients"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:text="@string/btn_add_ingredient"
            android:onClick="addIngredients"/>

Java:

public void addIngredients(View view) {

    final String ADDINGREDIENT = "ADD INGREDIENT";

    Log.d(ADDINGREDIENT, ADDINGREDIENT);

    LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout_ingredients);

    LinearLayout ingredientLayout = new LinearLayout(this);
    ingredientLayout.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    ingredientLayout.setOrientation(LinearLayout.HORIZONTAL);
    ingredientLayout.setWeightSum(3f);

    EditText editTextIngredient = new EditText(this);
    editTextIngredient.setHint("Ingredient");
    editTextIngredient.setLayoutParams(new LinearLayout.LayoutParams(
            0,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            1f));

    EditText editTextAmount = new EditText(this);
    editTextAmount.setHint("Amount");
    editTextAmount.setLayoutParams(new LinearLayout.LayoutParams(
            0,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            1f));

    EditText editTextUnit = new EditText(this);
    editTextAmount.setHint("Unit");
    editTextAmount.setLayoutParams(new LinearLayout.LayoutParams(
            0,
            LinearLayout.LayoutParams.WRAP_CONTENT,
            1f));

    ingredientLayout.addView(editTextIngredient);
    ingredientLayout.addView(editTextAmount);
    ingredientLayout.addView(editTextUnit);

    linearLayout.addView(ingredientLayout);

}

最佳答案

这里的问题是,您将加权布局添加到不在屏幕上的布局,因此在添加它们时其宽度未知。

您有几种方法,但最简单的一种可能是获取屏幕上已有元素的宽度并简单地复制它们(因为您一开始就使用加权布局)。

您还可以测量屏幕并使用该测量值来设置每个布局的宽度。

最后,您可以在添加父级并且宽度已知后使用 ViewTreeObserver 添加它们(请参阅此处的帖子: Getting the width/height of a layout in Android )。

最后一个是最“有趣”的,但效率最低且最有用,因为您的原始布局已经完成了计算。

关于java - 以编程方式添加带有权重的 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30419588/

相关文章:

java - Spring启动@Value NullPointerException

java - Android ProgressDialog 显示何时检索数据

java - 保存使用 onTouchEvent 绘制到位图上的图像

java - 我可以像在 Visual Studio 上一样在 Eclipse 上创建项目模板吗

java - 如何将 BindingResult 作为请求参数传递?

java - 圆上的三个随机点

android - 按下时底部导航栏中的彩色图标

Android Studio 看不到 SDK 目录中存在的某些 .java 文件

Android 签名配置和与 splits abi 的冲突

android - 为 Android 开发时如何在 Eclipse 中看到 Action Bar?