【问题标题】:Problem with LayoutInflator to set TextView AndroidLayoutInflator 设置 TextView Android 的问题
【发布时间】:2011-07-17 16:53:39
【问题描述】:

大家好,我使用两种布局,一种通过主布局文件中的“包含”嵌入。 我希望在主 xml 的活动中的嵌入式设置中设置 TextView。到目前为止,这是我想出的......

主要 xml:date_list_layout.xml

<RelativeLayout android:id="@+id/RelativeLayout01"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:background="@drawable/bgoption">

<include layout="@layout/cur"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />

</RelativeLayout>

嵌入的xml:cur.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/currency"  
  android:textSize="14px"
  android:paddingLeft="10dp"
  android:textColor="#d17375"
  ></TextView>

</LinearLayout>

然后我的 Activity 代码将内容设置为主 xml,但尝试膨胀嵌入的内容以将文本设置如下......

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.date_list_layout);

View v  = LayoutInflater.from(getBaseContext()).inflate(R.layout.cur,
            null);

    TextView currency = (TextView) v.findViewById(R.id.currency);
    currency.setText("test");

}

我没有收到任何错误,但 TextView 仍然为空。任何想法我做错了什么

谢谢 一个

【问题讨论】:

  • 尝试直接使用视图作为TextView currency = (TextView) findViewById(R.id.currency);(去掉“View”参考v),它可能对你有帮助,你可以获得包含的布局直接信息
  • 是的,确实如此,谢谢。
  • 正如@riser 指出的那样:一旦你有了included cur.xml,你就不再需要手动充气了。它会自动插入到结构中,因此您可以安全地删除额外的 LayoutInflater 调用。

标签: android layout textview android-inflate


【解决方案1】:

你可以直接在TextView中使用setText。无需在活动中调用 LayoutInflater。

setContentView(R.layout.date_list_layout); TextView 货币 = (TextView) v.findViewById(R.id.currency); 货币.setText(“测试”);

【讨论】:

    【解决方案2】:

    直接使用TextView:

     TextView currency = (TextView) findViewById(R.id.currency);
    

    一旦你有了included cur.xml,你就不再需要手动充气了。它会自动插入到结构中,因此您可以安全地删除额外的 LayoutInflater 调用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      相关资源
      最近更新 更多