简体   繁体   English

如何以编程方式隐藏 android XML output 中的一些但不是全部查看项目?

[英]How to programmatically hide android some, but not all, view items in an XML output?

I'm trying to program a disc golf scoring app on Eclipse for my android phone.我正在尝试为我的 android 手机在 Eclipse 上编写一个圆盘高尔夫计分应用程序。 I'd like to set it up for up to 6 players, but mostly 2 people will use it for a game.我想为最多 6 名玩家设置它,但大多数人会在游戏中使用它。 The data is being stored in a sqlite DB, and I am using a SimpleCursorAdapter to populate the data for holes that have already been scored.数据存储在 sqlite DB 中,我正在使用 SimpleCursorAdapter 填充已评分孔的数据。 here is that code:这是代码:

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

From searching the internet I've found what I think are two posibilities that SHOULD work, but do not.通过搜索互联网,我发现了我认为应该可行但不可行的两种可能性。

First I've tried the XML Attribute: android.visibility.首先,我尝试了 XML 属性:android.visibility。 It looks like this in the PORTION of the view that I am trying to "test" hide:在我试图“测试”隐藏的部分视图中看起来像这样:

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

I have tried it with "GONE", "Gone" and "gone".我已经用“GONE”、“Gone”和“gone”试过了。 NONE of them work in the eclipse emulator OR on my actual phone.它们都不能在 eclipse 模拟器或我的实际手机上工作。 So, there is no point in trying to parameterize this attribute.因此,尝试参数化此属性是没有意义的。

Next I've tried setting the XML attribute for android:layout_height to "0dip".接下来,我尝试将 android:layout_height 的 XML 属性设置为“0dip”。 This indeed works in the emulator and on my phone WHEN IT IS HARDCODED.当它被硬编码时,这确实适用于模拟器和我的手机。

Then I moved to the next logical step (as I see it), storing a parameter in the DB so that I can "show" or "not show" the item DEPENDING on conditions within the record.然后我转到下一个逻辑步骤(如我所见),在数据库中存储一个参数,以便我可以根据记录中的条件“显示”或“不显示”项目。 So, I've stored a field in the DB with two values "0dip" and "wrap_content".因此,我在数据库中存储了一个字段,其中包含两个值“0dip”和“wrap_content”。 I pass these to the layout as shown in the java above as R.id.skip.我将这些传递给布局,如上面的 java 所示,为 R.id.skip。 I've also added these to the output just to audit that they are really there.我还将这些添加到 output 只是为了审核它们是否真的存在。 Here is that XML:这是 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"/>

In the above test, both via the Eclipse emulator and my android phone, the last TextView confirms that the DB contains either "0dip" or "wrap_content", BUT the LinearLayout with:在上述测试中,通过 Eclipse 仿真器和我的 android 手机,最后一个 TextView 确认 DB 包含“0dip”或“wrap_content”,

      android:layout_height="@+id/skip">

behaves as if it were "0dip" ALL of the TIME.表现得好像它一直都是“0dip”。 In other words, I cannot PROGRAMMATICALLY" affect the XML attribute for android:layout_height.换句话说,我不能以编程方式”影响 android:layout_height 的 XML 属性。

If there is a better/more standard way of accomplishing what I am trying to do, please share - BUT BE CLEAR.如果有更好/更标准的方式来完成我想做的事情,请分享 - 但要清楚。 I am new, so CODE EXAMPLES wwill work best for me.我是新手,所以代码示例最适合我。


May 29th - It seems to me (based on testing) that you cannot alter layout attributes for the layout specified in this code: 5 月 29 日 - 在我看来(基于测试)您无法更改此代码中指定的布局的布局属性:

SimpleCursorAdapter notes = new SimpleCursorAdapter(this, 
                                                    R.layout.hole_info, 
                                                    notesCursor, from, to);
setListAdapter(notes);

Anything I try leads to some error ort another.我尝试的任何事情都会导致一些错误或另一个。 So, I've seen examples of custom list adapters where these attributes are altered, so I'm trying to convert to a custom list adapter.因此,我已经看到了更改这些属性的自定义列表适配器的示例,因此我正在尝试转换为自定义列表适配器。

Why not do it in code?为什么不在代码中做呢?

LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id);
ll.setVisibility(View.GONE);

Your XML layout code您的 XML 布局代码

android.visibility="GONE"

should be应该

android:visibility="GONE"

Change visible of a LinearLayout like Gabriel Neguţ say:更改 LinearLayout 的可见性,例如 Gabriel Neguţ 说:

LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id); LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id); ll.setVisibility(View.GONE); ll.setVisibility(View.GONE);

or change height of LinearLayout:或更改 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM