2

我需要以编程方式创建 UI 控件,但我的 EditText 没有出现在屏幕上。

这是一种骨架 xml,我将在其中以编程方式添加表行。

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:orientation="vertical"
        android:paddingLeft="20dp"
        android:id="@+id/main_layout">

        <TableLayout android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:id="@+id/myTable" >

        </TableLayout>


    </LinearLayout>

</ScrollView>

这是我活动中的java代码,

TableLayout mainTable = (TableLayout) findViewById(R.id.myTable);
TableRow row = new TableRow(this);

TextView textView = new TextView(this);
textView.setText("Text1");
row.addView(textView);

EditText editText = new EditText(this, null, R.style.editTextStyle);
row.addView(editText);

mainTable.addView(row);

我的 editTextStyle 如下,

<style name="editTextStyle">
    <item name="android:paddingTop">10dp</item>
    <item name="android:paddingLeft">7dp</item>
    <item name="android:textSize">27sp</item>
    <item name="android:layout_width">150dp</item>
<item name="android:layout_height">50dp</item>
</style>

textView 正常显示,但 editText 没有。请问有什么想法吗?

4

1 回答 1

1

如果您使用的是 Android 4.0.3,那么在您的 LinearLayout 中删除

android:focusable="true"
android:focusableInTouchMode="true"  

或者你可以为edittext设置任何背景颜色,即。

在你的风格中添加项目为

<item name="android:background">your required color </item>

然后你就可以看到edittext了。

您也可以在活动中执行以下操作

EditText editText = new EditText(this);
editText.setBackgroundColor(R.color.BLUE);
editText.setTextColor(R.color.BLACK);
row.addView(editText);

在 color.xml 中

<resources>
<color name="Black">#000000</color>
<color name="Blue">#FF006767</color>
</resources>
于 2012-08-01T10:52:09.080 回答