【问题标题】:Set the same height to a TextView and an EditText in a row of a TableLayout?为 TableLayout 的一行中的 TextView 和 EditText 设置相同的高度?
【发布时间】: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


【解决方案1】:

回答我自己的问题:

问题解决了:android:baselineAligned="false" 将此行放入此 XML 中即可!

 <!-- 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"
    android:baselineAligned="false">
</TableRow>

感谢您的帮助!

【讨论】:

    【解决方案2】:

    android:layout_height="match_parent" 用于XML 中的TextView,并删除setHeight() 用于TextView 中的Activity。由于您正在手动设置EditText 的大小,因此无需更改它。通过这样做TextView 将占用height 的所有空间。

    编辑

    <!-- 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:layout_width="60dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            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:layout_width="60dp"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:singleLine = "false" >
    </TextView>
    

    尝试将layout_height 设置为0dp 并将layout_weight 设置为1

    如果您想要两个 Views 的高度相同,那么 layout_weight 可能会有所帮助。

    【讨论】:

    • 非常感谢您的帮助!不幸的是,TextView 不是方形的。我会用新的屏幕截图链接到问题
    • 我试过这个,但我不明白出了什么问题。我已经使用 android:layout_width="60dp" 选项复制/粘贴了您的 XML,但单元格的宽度与其中的文本一样宽。现在我已经在没有TextViews 而只有EditText 的情况下对其进行了测试,我发现问题出在textSize 上。如果我在没有选项android:textSize="6.5sp" 的情况下制作 TextView,它就可以工作。但不幸的是,我需要这么小的文本。
    • 如果您需要小字体,可以试试android:textAppearance="?android:attr/textAppearanceSmall"
    • 使用该命令,文本显示为 14sp,这对于 TextView 来说太大了。我现在将使用其他布局进行测试,也许这会起作用。如果您对如何将 TextView 放入 TableRow 有其他想法,请告诉我。感谢您的帮助!
    猜你喜欢
    • 2014-03-27
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2011-02-16
    相关资源
    最近更新 更多