[英]Set visibility of TextView in android not working
當我嘗試將視圖設為“ 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文件中。 我正在使用兩個,因為我有一個帶有標題和圖像等的列表視圖。
關於使視圖“不可見”或“消失”有什么想法?
謝謝,
本
試試這個代碼
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);
如果這不起作用,請發布您遇到的異常。
你不能只有一個帶有textview和listview的xml嗎? 那么它應該工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.