【问题标题】:How to set texts in android for Tablelayouts?如何在 android 中为 Tablelayouts 设置文本?
【发布时间】:2014-10-05 18:52:41
【问题描述】:

我正在尝试为表格布局中的文本视图设置一些值,但由于应用程序自动关闭,我无法在手机上对其进行测试。

我的 XML 文件在这里定义:

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableAfc"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="1">
   <TableRow
       android:id="@+id/rowAf1"
   >
     <TextView
         android:text="Litera"
         android:singleLine="true"
        android:id="@+id/tAf1"
        android:padding="5dip" />
    <TextView
        android:text="Pozitie initiala"
        android:singleLine="true"
        android:id="@+id/tAf2"
        android:gravity="center"
        android:padding="5dip" />
    <TextView
        android:text="Pozitie mediana"
        android:id="@+id/tAf3"
        android:singleLine="true"
        android:gravity="center"
        android:padding="5dip" />
     <TextView
        android:text="Pozitie finala"
        android:singleLine="true"
        android:id="@+id/tAf4"
        android:gravity="center"
        android:padding="5dip" />
    </TableRow>
  </TableLayout>

我的java代码...

 public void onCreate(Bundle icicle) {
       super.onCreate(icicle);
       setContentView(R.layout.cons_africate);
       TextView t1= (TextView) super.findViewById(R.id.tAf1);
       t1.setText(1);
   }

如果我不尝试更改 java 代码中的文本,它可以工作,但是使用这两行它会崩溃......

【问题讨论】:

  • 为了将来参考,您可能希望发布您在运行代码并崩溃时遇到的堆栈跟踪或异常

标签: java android xml tablelayout settext


【解决方案1】:

t1.setText(1); 不起作用,因为setText() 只接受 CharSquence 类型。所以使用t1.setText("1");

【讨论】:

  • 不仅有setText(Charsequence) 方法。还有一个setText(int) 方法,但是 int 参数应该是一个字符串资源 id。
  • 如果您查看 R.java,其中所有带有布局和 strings.xml 的静态属性都以 Char 序列形式返回,您正在谈论的是 setText(int resId)。但是'resId'必须与返回字符序列的strings.xml中引用的一样。
【解决方案2】:

在 settext 中使用倒置逗号,即“1”

【讨论】:

  • 这不是答案
猜你喜欢
  • 2013-01-05
  • 2015-01-27
  • 2014-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
相关资源
最近更新 更多