【发布时间】:2013-07-12 09:54:23
【问题描述】:
我需要一个带有 TextViews 和 EditTexts 的 TableLayout,它应该像一个填字游戏。
我已经用LinearLayout, width=0dp and weight=1, 对此进行了测试,但它并没有像我想要的那样工作。然后我用 TableLayout 试过了,我想我已经接近解决方案了。我已经在 XML 或 Java 中测试了高度和宽度,但它不起作用。我现在的问题是所有 TextView 都比 EditText 薄,我不知道为什么以及如何使它们看起来一样。
XML 文件:
<!-- activity_sraetsel_main.xml -->
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</TableLayout>
<!-- activity_sraetsel_row.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableRow"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center_horizontal">
</TableRow>
<!-- activity_sraetsel_edittext.xml -->
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/cell_border"
android:id="@+id/textView"
android:inputType="textCapCharacters"
android:gravity="center_horizontal"
android:maxLength="1">
</EditText>
<!-- activity_sraetsel_textview.xml -->
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/cell_border"
android:id="@+id/textView"
android:textSize="6.5sp"
android:singleLine = "false" >
</TextView>
<!-- cell_border.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="1dp" android:color="#515151"/>
<padding android:left="10dp" android:top="5dp"
android:right="10dp" android:bottom="5dp" />
</shape>
还有java部分,我把它们放在一起:
TableLayout table = (TableLayout) this.findViewById(R.id.table);
for (int y = 0; y < hoehe; y++) {
// Inflate your row "template" and fill out the fields.
TableRow row = (TableRow) LayoutInflater.from(this).inflate(R.layout.activity_sraetsel_row, null);
for (int x = 0; x < breite; x++) {
if (raetsel[y][x].startsWith(",", 1)) {
EditText eText = (EditText) LayoutInflater.from(this).inflate(R.layout.activity_sraetsel_edittext, null);
if (!(raetsel[y][x].startsWith("a"))) {
eText.setText(raetsel[y][x].substring(0, 1));
} //if
eText.setWidth(60);
eText.setHeight(60);
row.addView(eText);
Log.d(TAG, "SRaetselMain, AntwortFeld erstellt");
} else {
TextView vText = (TextView) LayoutInflater.from(this).inflate(R.layout.activity_sraetsel_textview, null);
vText.setText(raetsel[y][x].substring(0, raetsel[y][x].indexOf("|")));
vText.setWidth(60);
vText.setHeight(60);
row.addView(vText);
Log.d(TAG, "SRaetselMain, FrageFeld erstellt");
} //if
} //for
table.addView(row);
} //for
table.requestLayout();
最后是这样的:
EDIT1:
使用android:layout_height="match_parent" 用于 XML 中的 TextView 而没有 setHeight() 用于 Activity 中的 TextView:
http://i.stack.imgur.com/btzj4.png
EDIT2:
使用android:layout_weight="1" 和android:layout_height="0dp",它看起来像EDIT1
现在我已经在没有 TextViews 并且仅使用 EditText 的情况下对其进行了测试,我发现问题出在 textSize 上。如果我在没有选项android:textSize="6.5sp" 的情况下制作 TextView,它会起作用。但是文本太大了,不幸的是我需要这个文本那么小。
EDIT3: 我已经用 LinearLayout 和两个 TextViews 试过了。 我真的不知道如何改变这两个视图处于同一高度。
http://i.stack.imgur.com/wIENf.png
感谢您在这件事上的帮助。
【问题讨论】:
-
屏幕截图链接失效
标签: android android-layout android-edittext textview android-tablelayout