【问题标题】:Change the text size of a TextView and a Column in TableLayout in Android在 Android 的 TableLayout 中更改 TextView 和 Column 的文本大小
【发布时间】:2016-07-26 10:06:59
【问题描述】:

This is the layout

如您所见,我正在尝试拉伸文本框,使其位于“NOPE”按钮旁边。我受到列大小和文本框大小的限制,我不知道如何更改。

XML:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/editBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="36dp"
        android:layout_column="2"
        android:onClick="editEvent"
        android:text="EDIT" />

    <TextView
        android:id="@+id/attendingEventInListviewRow"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_column="38"
        android:height="30sp"
        android:tag="Events2goName"
        android:text="Events2go"
        android:textColor="#000000"
        android:textSize="22sp"
        android:textStyle="normal" />

    <Button
        android:id="@+id/cancel_arrive"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="130dp"
        android:layout_height="36dp"
        android:layout_column="37"
        android:layout_gravity="right"
        android:layout_weight="0.08"
        android:onClick="selectCancelArrive"
        android:text="Nope" />
</TableRow>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</TableRow>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</TableRow>

【问题讨论】:

  • 你的TextView 大小固定为200dp,也许改成0dp 并在那里添加android:weight="1"
  • 按照我说的,TextView 将占据TableRow 中的所有剩余空间
  • 我尝试将尺寸更改为 250dp(不增加重量),它只是按下了“NOPE”按钮。比我试过你说的,结果是:prntscr.com/bxnmgm
  • 那个按钮有layout_weight也有width,试试去掉权重,为什么你把layout_column设为37?

标签: java android layout text


【解决方案1】:

如果您使用dp 使用硬代码设置宽度,它将无法在多个屏幕上正常工作。我不知道你测试你的应用程序的屏幕尺寸..你可以设置TextViewandroid:layout_width="100dp"我希望它对你有用..

但最好的是

您可以在TableRow 中设置android:weightSum 并在TableRow 下设置android:layout_weight Views ... 下面的代码已为您完成。它适合所有屏幕尺寸..

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:weightSum="10">

    <Button
        android:id="@+id/editBtn"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="36dp"
        android:layout_column="2"
        android:layout_weight="2"
        android:onClick="editEvent"
        android:text="EDIT" />

    <TextView
        android:id="@+id/attendingEventInListviewRow"
        android:layout_width="0dp"
        android:layout_weight="6"
        android:layout_height="wrap_content"
        android:layout_column="38"
        android:height="30sp"
        android:tag="Events2goName"
        android:text="Events2go"
        android:textColor="#000000"
        android:textSize="22sp"
        android:textStyle="normal" />

    <Button
        android:id="@+id/cancel_arrive"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="36dp"
        android:layout_column="37"
        android:layout_gravity="right"
        android:layout_weight="0.08"
        android:onClick="selectCancelArrive"
        android:text="Nope" />
</TableRow>

【讨论】:

    【解决方案2】:

    试试这个……

    <TextView
                android:id="@+id/attendingEventInListviewRow"
                android:layout_width="0dp"
                android:layout_weight=""
                android:layout_height="wrap_content"
                android:layout_column="38"
                android:height="30sp"
                android:tag="Events2goName"
                android:text="Events2go"
                android:textColor="#000000"
                android:textSize="22sp"
                android:textStyle="normal" />
    

    【讨论】:

      【解决方案3】:

      例如,如果您有 3 列(100% 权重)并且您希望第一列使用总权重的 40%,则输入 android:layout_weight="0.4",如果第二列使用 50%,则输入 android:layout_weight="0.5" 并且第三列和最后一列使用 10% 默认 android:layout_weight="0.1"

      【讨论】:

        猜你喜欢
        • 2012-02-05
        • 1970-01-01
        • 2014-06-04
        • 1970-01-01
        • 2019-08-14
        • 2015-02-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-20
        相关资源
        最近更新 更多