eclipse 如何以编程方式隐藏 android XML 输出中的一些(但不是全部)视图项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6163039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 16:21:59  来源:igfitidea点击:

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

javaandroidxmleclipseandroid-layout

提问by Tom Wruble

I'm trying to program a disc golf scoring app on Eclipse for my android phone. I'd like to set it up for up to 6 players, but mostly 2 people will use it for a game. 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. here is that code:

我正在尝试在 Eclipse 上为我的 android 手机编写一个光盘高尔夫评分应用程序。我想为最多 6 名玩家设置它,但大多数情况下有 2 人会在游戏中使用它。数据存储在一个 sqlite 数据库中,我使用 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);
}

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. It looks like this in the PORTION of the view that I am trying to "test" hide:

首先,我尝试了 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>

I have tried it with "GONE", "Gone" and "gone". NONE of them work in the eclipse emulator OR on my actual phone. So, there is no point in trying to parameterize this attribute.

我已经用“GONE”、“Gone”和“gone”试过了。它们都不能在日食模拟器或我的实际手机上工作。所以,尝试参数化这个属性是没有意义的。

Next I've tried setting the XML attribute for android:layout_height to "0dip". This indeed works in the emulator and on my phone WHEN IT IS HARDCODED.

接下来,我尝试将 android:layout_height 的 XML 属性设置为“0dip”。当它被硬编码时,这确实适用于模拟器和我的手机。

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". I pass these to the layout as shown in the java above as R.id.skip. I've also added these to the output just to audit that they are really there. Here is that XML:

然后我转到下一个逻辑步骤(如我所见),在数据库中存储一个参数,以便我可以根据记录中的条件“显示”或“不显示”项目。因此,我在数据库中存储了一个字段,其中包含两个值“0dip”和“wrap_content”。我将这些传递给布局,如上面的 java 中所示的 R.id.skip。我还将这些添加到输出中只是为了审核它们是否真的存在。这是那个 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 确认数据库包含“0dip”或“wrap_content”,但 LinearLayout 包含:

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

behaves as if it were "0dip" ALL of the TIME. In other words, I cannot PROGRAMMATICALLY" affect the XML attribute for android:layout_height.

表现得好像它一直都是“0dip”。换句话说,我不能以编程方式”影响 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.

我尝试的任何事情都会导致一些错误或另一个错误。因此,我已经看到了更改这些属性的自定义列表适配器的示例,因此我正在尝试转换为自定义列表适配器。

回答by Gabriel Negut

Why not do it in code?

为什么不在代码中做呢?

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

回答by xtr

Your XML layout code

您的 XML 布局代码

android.visibility="GONE"

should be

应该

android:visibility="GONE"

Change visible of a LinearLayout like Gabriel Negu? say:

更改像 Gabriel Negu 这样的 LinearLayout 的可见性?说:

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

LinearLayout ll = (LinearLayout)findViewById(R.id.your_layout_id); 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);

回答by marienke

What about this guy's solution http://enjoyandroid.wordpress.com/2012/03/12/customizing-simple-cursor-adapter/

这家伙的解决方案怎么样http://enjoyandroid.wordpress.com/2012/03/12/customizing-simple-cursor-adapter/

Kind of worked for me.

有点对我来说有效。