android - 发表观点" GONE"导致下面的视图也是不可见的

时间:2015-01-08 01:41:04

标签: android listview spinner relativelayout visibility

我是Android的新手,我在应用程序上工作了几个星期。

正如标题所说,我遇到的问题是当我想以编程方式将Spinner设置为GONE时,ListView下面的RelativeLayout也已消失。

这是xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/tableSelector"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <ListView
        android:id="@+id/insertList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tableSelector"
        android:layout_above="@+id/saveButton"
        android:background="#aaffffff"
        android:divider="#11000000"
        android:dividerHeight="4dp"
        android:visibility="gone" >

    </ListView>

   <Button
        android:id="@+id/saveButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:text="@string/button_save"
        android:visibility="visible" />

</RelativeLayout>

以下是我如何以编程方式设置它:

switch(spinnerFlag)
{
    case SPINNER_HIDDEN: tableSelector.setVisibility(View.GONE); break;
    case SPINNER_VISIBLE: tableSelector.setVisibility(View.VISIBLE); break;
    case SPINNER_VISIBLE_DISABLED: tableSelector.setVisibility(View.VISIBLE); 
                                      tableSelector.setEnabled(false); 
                                      break;
    default: Log.d("onActivityCreated - InserFragment", "Spinner flag < " + spinnerFlag + " > does not exist. Spinner is now visible enabled.");
}

这适用于案例SPINNER_VISIBLESPINNER_VISIBLE_DISABLED,但在将其设置为SPINNER_HIDDEN时,ListView也会消失。

已更新  正如Psy Duck和Nguyen Doan Tung建议我将layout_alignWithParentIfMissing属性添加到ListView。但是我注意到它仍然没有用。我在xml中将ListView的可见性设置为visible并显示,但它是空的。  所以它解决了我遇到的问题,但是现在我的另一个问题是ListView为空(我再次提到当(spinnerFlag == SPINNER_VISIBLE || spinnerFlag == SPINNER_VISIBLE_DISABLED)时,它工作得很好)

以下是我的代码的更大部分:

public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        insertList = (ListView) getActivity().findViewById(R.id.insertList);
        /* Some code */

        tableSelector = (Spinner) getActivity().findViewById(R.id.tableSelector);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item,tables);
        tableSelector.setAdapter(adapter);
        tableSelector.setOnItemSelectedListener(new OnItemSelectedListener() {
        /* Some code containing some Log.d() */
        });
        /* Some code */
        Log.d("Insert fragment", "Setting table selected by position in list " + pos + ", " + tName);

        tableSelector.setSelection(pos); // pos is calculated correctly in the code i didn't show

        switch(spinnerFlag)
        {
            case SPINNER_HIDDEN: tableSelector.setVisibility(View.GONE); break;
            case SPINNER_VISIBLE: tableSelector.setVisibility(View.VISIBLE); break;
            case SPINNER_VISIBLE_DISABLED: tableSelector.setVisibility(View.VISIBLE); 
                                              tableSelector.setEnabled(false); 
                                              break;
            default: Log.d("onActivityCreated - InserFragment", "Spinner flag < " + spinnerFlag + " > does not exist. Spinner is now visible enabled.");
        }
    }

Log之前的tableSelector.setSelection(pos)显示在LogCat中,但Logs中没有显示tableSelector.onItemSelected();,因此setSelection不会调用它。 任何想法为什么会这样? (我当时认为将View设置为GONE可能会停止它的功能,但是在设置为GONE之前我setSelection()

谢谢!

固定 而不是:

 tableSelector.setVisibility(View.GONE); break;

我使用了解决方法:

tableSelector.setVisibility(View.VISIBLE); 
tableSelector.setEnabled(false);
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) tableSelector.getLayoutParams();
params.height = 0;
tableSelector.setLayoutParams(params);
break;

它现在可以正常运作。

2 个答案:

答案 0 :(得分:3)

您需要将此行添加到列表视图android:layout_alignWithParentIfMissing="true"

<ListView
    android:id="@+id/insertList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tableSelector"
    android:layout_above="@+id/saveButton"
    android:background="#aaffffff"
    android:divider="#11000000"
    android:dividerHeight="4dp"
    android:layout_alignWithParentIfMissing="true"
    android:visibility="gone" >

</ListView>

答案 1 :(得分:2)

当视图设置为GONE时,它不会占用布局中的任何空间。您的ListView有此{* 1}},它会在android:layout_below="@+id/tableSelector"下方Spinner设置ListView

为了避免这种情况,你可以简单地将android:layout_alignWithParentIfMissing="true"行添加到ListView,这样它就会忽略Spinner缺失时的查找,并且也会覆盖它。希望它有所帮助。