在android中设置TextView的可见性不起作用

时间:2011-10-27 14:06:10

标签: java android xml listview textview

当我尝试将视图设为'GONE'时,我的程序会遇到运行时异常。 我的代码处理它:

setContentView(R.layout.list_main);
... 
 lv =  getListView();
             View header = (View) findViewById(R.id.header_layout_root);
             TextView tv = (TextView) header.findViewById(R.id.new_highscore);
            tv.setVisibility(View.GONE);

 lv = getListView();
        LayoutInflater inflater = getLayoutInflater();
        View header = inflater.inflate(R.layout.list_header, (ViewGroup) findViewById(R.id.header_layout_root));
        lv.addHeaderView(header, null, false);

mC = mDBHelper.getAllHighScores();
        startManagingCursor(mC);

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, mC, 
                new String[] { DBAdapter.COL_NAME, DBAdapter.COL_SCORE }, 
                new int[] { R.id.txtText1, R.id.txtText2 });
        lv.setAdapter(adapter);

的xml:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:id="@+id/header_layout_root"
  android:background="#ffffff"
  android:orientation="vertical">
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Pick Master High Scores"
    android:background="#000000"
    android:textColor="#ffffff"
    android:textSize="28dp"
    android:gravity="center_horizontal"
    android:paddingTop="10dp"
    android:paddingBottom="10dp">
</TextView>
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     android:id="@+id/header_layout_root1">
    <ImageView
        android:id="@+id/sample_image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/pickmaster"
        android:scaleType="fitXY">
    </ImageView>
    <TextView
        android:id="@+id/new_highscore"
        android:text="New High Score added!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffffff"
        android:textSize="12dp"
        android:background="#AA000000"
        android:padding="5dp"
        android:layout_alignParentLeft="true"
        android:layout_alignBottom="@id/sample_image">
    </TextView>
</RelativeLayout>

我认为问题是new_highscore不在list_main中,而是在另一个xml文件中。我正在使用两个,因为我有一个带有标题和图像等的列表视图。

任何关于让视图“隐形”或“消失”的想法?

谢谢,

2 个答案:

答案 0 :(得分:8)

试用此代码

   lv = getListView();
   LayoutInflater inflater = getLayoutInflater();
   View header = inflater.inflate(R.layout.list_header, (ViewGroup) findViewById(R.id.header_layout_root));
   lv.addHeaderView(header, null, false);
   View relativeLayout = header.findViewById(R.id.header_layout_root1);
   TextView highScore = relativeLayout.findViewById(R.id.new_highscore);
   highScore.setVisibility(View.GONE);

从顶部删除此代码

lv =  getListView();
View header = (View) findViewById(R.id.header_layout_root);
TextView tv = (TextView) header.findViewById(R.id.new_highscore);
tv.setVisibility(View.GONE);

如果这不起作用,请发布您遇到的例外情况。

答案 1 :(得分:-1)

你不能只有一个带有textview和listview的xml。它应该工作