问题
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