【问题标题】:EditText added programmatically differes from XML version以编程方式添加的 EditText 与 XML 版本不同
【发布时间】:2012-12-09 17:17:32
【问题描述】:

我有一个包含 EditText 和 2 个按钮的 XML 布局。如果我单击加号按钮,则会以编程方式添加一个新的编辑文本。这可行,但编辑文本看起来不同。根据XML,XML中定义的edittext没有任何特殊属性,所以我相信它不是一个特定的布局设置。

我的问题是如何使我以编程方式添加的 EditText 看起来一样?

包含数字的 EditText 是我以编程方式添加的 edittext。空的正在 XML 中创建。


(来源:tozz.nl

代码:

        LinearLayout baseLayout = (LinearLayout) findViewById(R.id.baseLayout);

        LinearLayout linearLayout = new LinearLayout(getApplicationContext());
        linearLayout.setId(100 + numPlayers);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        EditText editText = new EditText(getApplicationContext());
        editText.setText(editText.toString().substring(25, 30));

        ImageButton delButton = new ImageButton(getApplicationContext());
        delButton.setImageResource(R.drawable.ic_delete);

        linearLayout.addView(editText);
        linearLayout.addView(delButton);

        baseLayout.addView(linearLayout);

我的 XML 如下:

    <LinearLayout
        android:id="@+id/linearPlayer1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/editPlayer1"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:gravity="center_vertical" />



        <ImageButton
            android:id="@+id/addPlayer1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_input_add" />

    </LinearLayout>

【问题讨论】:

  • 发布您制作的代码并添加额外的EditTexts
  • 很简单:EditText editText = new EditText(getApplicationContext()); editText.setText(editText.toString().substring(25, 30));
  • 您将它们添加到哪种类型的布局中以及如何添加?

标签: android layout android-edittext


【解决方案1】:

Luksprog 回答了我的问题:

在创建新视图时传递活动上下文而不是应用程序上下文。

【讨论】:

    【解决方案2】:

    使用正确的LayoutParams 添加这些视图应该使EditText 与布局中的初始视图一样:

        linearLayout.addView(editText, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));
        linearLayout.addView(delButton, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
    
        baseLayout.addView(linearLayout, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
    

    【讨论】:

    • 谢谢,但这不是我的意思。使用您的代码,布局(宽度、右侧按钮等)是固定的。但是 EditText 仍然有不同的风格。请看新的截图。我的意思是 XML 编辑文本只有一条整洁的行作为输入,而我添加的编辑文本是灰色框。 !new screenshot
    • @user1750795 你能发布EditText的xml布局代码吗?添加的EditTexts 看起来像是禁用的EditText 框(setEnabled(false) 在您的代码中某处?)。你在你的应用中使用了一些库吗?
    • @user1750795 你还没有回答我之前评论中的所有问题。在创建新视图时,还要传递 Activity Contextnot Application Context。还要提及您正在测试的 Android 版本。
    • 啊!您关于活动上下文而不是应用程序上下文的评论修复了它。我已将 getApplicationContext() 更改为 v.getContext() 并解决了我的问题。非常感谢您的时间和精力!
    猜你喜欢
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多