[英]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.