使用inflater时,setText为TextView

时间:2013-07-25 14:50:04

标签: android android-layout textview

在我的Android应用程序中,我一直在使用RelativeLayout和inflater。所以我需要在textView中设置文本,我和Inflater一起使用,但我不能按代码设置文本。

XML文件activity_title

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent"
android:background="#000000" >

<TextView
    android:id="@+id/text_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:text="Text" />

</RelativeLayout>

2 xml文件

<RelativeLayout
            android:id="@+id/rel_container"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/linearLayout1"
            android:layout_toRightOf="@+id/linearLayout1"
             >
</RelativeLayout>

所以这是使用inflater的代码

relativeLayout = (RelativeLayout) findViewById(R.id.rel_container);
    ((ViewGroup) relativeLayout).removeAllViews();
    for (int i = 0; i < titleCalendar.length; i++) {
        getApplicationContext();
        vi= (LayoutInflater)
  getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.activity_title, null);  
        textViewC = (TextView) v.findViewById(R.id.text_title);
        textViewC.setText("sdfsfasfasfasfasf");
        textViewC.setTextColor(Color.parseColor("#ffffff"));


        final RelativeLayout.LayoutParams params = new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

        params.addRule(RelativeLayout.BELOW, addcnvrtTime);


        textViewC.setLayoutParams(params);
        ((ViewGroup) relativeLayout).addView(v, 0, params);

    }

我无法看到我在该textview上设置的文字

2 个答案:

答案 0 :(得分:2)

这是为了获得Inflater元素:

LayoutInflater inflater = getLayoutInflater();
View customView = inflater.inflate(R.layout.YOUR_LAYOUT_INFLATER, null);
TextView text = (TextView)customView.findViewById(R.id.YOUR_TEXT_VIEW);
text.setText("BlaBlaBlaBlaBla");

为什么不将TextView直接放在Layout中,只需执行

TextView text = (TextView)findViewById(R.id.YOUR_TEXT_VIEW);
text.setText("BlaBlaBlaBlaBla");

答案 1 :(得分:0)

尝试删除params.addRule(RelativeLayout.BELOW,addcnvrtTime)?