将ImageView设置为可见后,ImageView未显示

时间:2015-03-18 16:57:12

标签: android android-layout imageview layout-inflater

我有一个带有两个图像视图的xml文件,我已将两者的可见性设置为已消失,因此我可以稍后通过java动态更改其可见性。但是当我将它们设置为可见时它们没有显示出来。这是我根据另一个活动发送的意图值将图像设置为可见的代码,

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View temp = inflater.inflate(R.layout.notes_row, container, false);
        if (getActivity().getIntent().getStringExtra("category")!=null && getActivity().getIntent().getStringExtra("category").equalsIgnoreCase("important")){
            ImageView imageView = (ImageView) temp.findViewById(R.id.icon2);
            imageView.setVisibility(View.VISIBLE);

    }
        else {
            ImageView imageView = (ImageView) temp.findViewById(R.id.icon); //draft
            imageView.setVisibility(View.VISIBLE);
        }

        mSimpleCursorAdapter=new SimpleCursorAdapter(getActivity(),R.layout.notes_row,null, from, to,0);

        View rootView = inflater.inflate(R.layout.fragment_todo_list, container, false);
        getLoaderManager().initLoader(LOADER_ID, null, this); //once this is done onCreateLoader will be called.
        final ListView listView = (ListView) rootView.findViewById(R.id.notes_list); //findViewById must be called using the rootView because we are inside a fragment.
         text=  (TextView) rootView.findViewById(R.id.empty_list);

        if(mSimpleCursorAdapter.getCount()==0) {

           text.setVisibility(View.VISIBLE);
        }


            if (getActivity().findViewById (R.id.fragment_container)!=null){
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);}//end if.

        listView.setAdapter(mSimpleCursorAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

                Cursor cursor = mSimpleCursorAdapter.getCursor();
                if (cursor != null && cursor.moveToPosition(position)) {

                    String category= cursor.getString(1);
                    String summary= cursor.getString(2);
                    String description=cursor.getString(3);
                    long id= cursor.getLong(cursor.getColumnIndex(NotesContract.NotesTable._ID));
                    int locationId= cursor.getInt(cursor.getColumnIndex(NotesContract.NotesTable.COLUMN_LOCATION));

                    String [] retrievedData= {category, summary, description};


                    if (getActivity().findViewById (R.id.fragment_container)!=null){
                        //two pane layout:
                        listView.setItemChecked(position, true);
                        listView.setBackgroundColor(Color.BLUE);



                        Bundle args = new Bundle();
                        args.putStringArray("data",retrievedData);
                        /*args.putInt("update", 1);*/
                        args.putLong("id", id);
                        args.putInt("locationId", locationId);
                        mCallback.onlistElementClicked(args );/*this is available in the parent activity*/
                    }

                    else {
                       // one pane layout:

                        Intent intent = new Intent(getActivity(), NotesDetails.class);
                        intent.putExtra(Intent.EXTRA_TEXT, retrievedData);
                        /*intent.putExtra("update", 1); */ //to indicate that the query should be update not insert.
                        intent.putExtra("id", id);
                        intent.putExtra("locationId", locationId); //whether it is 0 or 1
                        startActivity(intent);
                    }


                }//end outer cursor if.
            }
        });

        return rootView;
    }

这是我试图更改其中图像的布局的xml文件,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="30dp"
            android:layout_height="24dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/drafticon"
            android:visibility="gone">
        </ImageView>

        <ImageView
            android:id="@+id/icon2"
            android:layout_width="30dp"
            android:layout_height="24dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="8dp"
            android:layout_marginTop="8dp"
            android:background="@drawable/staricon"
            android:visibility="gone">
        </ImageView>



        <TextView
            android:id="@+id/label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="6dp"
            android:lines="1"
            android:text="@+id/TextView01"
            android:textSize="24dp"
            >
        </TextView>
    </LinearLayout>

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/> <!--text will be set dynamically -->
    </LinearLayout>

可以看出,包含图像视图的布局称为notes_row,我只将它用作SimpleCursorAdapter的参数。

任何人都可以帮帮我吗?我已经尝试了太多方法而且没有任何效果,我已经检查了if语句中的意图并且它不是null。

感谢任何帮助。

谢谢。

1 个答案:

答案 0 :(得分:0)

不要将可见度更改为已消失。对图像视图进行以下更改将其更改为不可见而不是消失。在您的XML替换中

android:visibility="gone"

使用

android:visibility="invisible"

尝试替换

android:background="@drawable/staricon"

使用

android:src="@drawable/staricon" 

您在水平线性布局中为textView指定匹配父级的宽度。这可能是您的图像视图无法显示的原因。

将文字视图的宽度更改为wrap_content,而不是match_parent

希望这对你有所帮助。