Set visibility of TextView in android not working

点点圈 提交于 2021-02-10 14:21:06

问题


My program runs into a runtime exception when I try to make the view 'GONE'. My code that handles it:

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>

The problem is, I think, that new_highscore is not in list_main, but it is in another xml file. I am using two as I have a listview with a header and image etc.

Any ideas on making the view 'invisible' or just 'gone'?

Thanks,

Ben


回答1:


Try this code

   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);

Remove this code from the top

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

If that doesn't work, please post the exception your are encountering.




回答2:


can't you have just one xml with a textview and a listview bellow . then it should work



来源:https://stackoverflow.com/questions/7917093/set-visibility-of-textview-in-android-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!