[英]How to programmatically hide android some, but not all, view items in an XML output?
我正在嘗試為我的 android 手機在 Eclipse 上編寫一個圓盤高爾夫計分應用程序。 我想為最多 6 名玩家設置它,但大多數人會在游戲中使用它。 數據存儲在 sqlite DB 中,我正在使用 SimpleCursorAdapter 填充已評分孔的數據。 這是代碼:
private void fillData() {
Cursor notesCursor = mDbHelper.fetchAllNotes();
startManagingCursor(notesCursor);
// Create an array to specify the fields we want to display in the list (only TITLE)
String[] from = new String[]{DiscGolfDbAdapter.KEY_HOLE,
DiscGolfDbAdapter.KEY_PAR,
DiscGolfDbAdapter.KEY_TOM_HOLE,
DiscGolfDbAdapter.KEY_TOM_GAME,
DiscGolfDbAdapter.KEY_CRAIG_HOLE,
DiscGolfDbAdapter.KEY_CRAIG_GAME,
DiscGolfDbAdapter.KEY_TOMS_POSITION,
DiscGolfDbAdapter.KEY_SKIP_PLAYER
};
// and an array of the fields we want to bind those fields to (in this case just text1)
int[] to = new int[]{R.id.schole, R.id.scpar, R.id.scth, R.id.sctg, R.id.scch, R.id.sccg, R.id.sctp,
R.id.skip};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.hole_info, notesCursor, from, to);
setListAdapter(notes);
}
通過搜索互聯網,我發現了我認為應該可行但不可行的兩種可能性。
首先,我嘗試了 XML 屬性:android.visibility。 在我試圖“測試”隱藏的部分視圖中看起來像這樣:
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android.visibility="GONE">
<TextView android:id="@+id/scch"
android:layout_width="45dip"
android:gravity="right"
android:textSize="25sp"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/sccg"
android:layout_width="45dip"
android:gravity="right"
android:textSize="25sp"
android:layout_height="wrap_content"/>
</LinearLayout>
我已經用“GONE”、“Gone”和“gone”試過了。 它們都不能在 eclipse 模擬器或我的實際手機上工作。 因此,嘗試參數化此屬性是沒有意義的。
接下來,我嘗試將 android:layout_height 的 XML 屬性設置為“0dip”。 當它被硬編碼時,這確實適用於模擬器和我的手機。
然后我轉到下一個邏輯步驟(如我所見),在數據庫中存儲一個參數,以便我可以根據記錄中的條件“顯示”或“不顯示”項目。 因此,我在數據庫中存儲了一個字段,其中包含兩個值“0dip”和“wrap_content”。 我將這些傳遞給布局,如上面的 java 所示,為 R.id.skip。 我還將這些添加到 output 只是為了審核它們是否真的存在。 這是 XML:
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="@+id/skip">
<TextView android:id="@+id/scch"
android:layout_width="45dip"
android:gravity="right"
android:textSize="25sp"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/sccg"
android:layout_width="45dip"
android:gravity="right"
android:textSize="25sp"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView android:id="@+id/skip"
android:gravity="center"
android:layout_width="315dip"
android:textSize="10sp"
android:layout_height="wrap_content"/>
在上述測試中,通過 Eclipse 仿真器和我的 android 手機,最后一個 TextView 確認 DB 包含“0dip”或“wrap_content”,
android:layout_height="@+id/skip">
表現得好像它一直都是“0dip”。 換句話說,我不能以編程方式”影響 android:layout_height 的 XML 屬性。
如果有更好/更標准的方式來完成我想做的事情,請分享 - 但要清楚。 我是新手,所以代碼示例最適合我。
5 月 29 日 - 在我看來(基於測試)您無法更改此代碼中指定的布局的布局屬性:
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.hole_info,
notesCursor, from, to);
setListAdapter(notes);
我嘗試的任何事情都會導致一些錯誤或另一個。 因此,我已經看到了更改這些屬性的自定義列表適配器的示例,因此我正在嘗試轉換為自定義列表適配器。
為什么不在代碼中做呢?
LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id);
ll.setVisibility(View.GONE);
您的 XML 布局代碼
android.visibility="GONE"
應該
android:visibility="GONE"
更改 LinearLayout 的可見性,例如 Gabriel Neguţ 說:
LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id); ll.setVisibility(View.GONE);
或更改 LinearLayout 的高度:
LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) ll.getLayoutParams();
lp.height = 0; // or lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
ll.setLayoutParams(lp);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.