简体   繁体   中英

ImageView is NOT SHOWING in a linear layout (android)

I'm creating an android application and got into issues already in the first stage.

I'm trying to display an image with an ImageView inside a Layout.

My code is as follows:

activity_starter.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".StarterActivity" >

    <ImageView
        android:id="@+id/starterPic"
        android:layout_width="0dp"
        android:layout_height="160dp"
        android:layout_gravity="center"
        android:layout_weight="0.84"
        android:adjustViewBounds="true"
        android:contentDescription="@string/app_name"
        android:src="@drawable/ic_launcher"
        android:visibility="visible" />

</LinearLayout>

and StarterActivity.java:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_starter);
        Log.i(LOG_TAG, "Entered Starter Screen");

        ImageView mImageView = (ImageView) findViewById(R.id.starterPic); 
        mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        mImageView.setImageResource(R.drawable.ic_launcher);
        mImageView.setVisibility(ImageView.VISIBLE);
        mImageView.invalidate();
}

The issues are:

  1. I can't hide the title bar on the top (black bar says "WhereIsIt" (app. name)).
  2. The image in the imageview is not shown.

I've been digging into this for hours, what am I doing wrong?

To hide the Title bar , you just need put this code in your onCreate method :

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE); //add this line 
            setContentView(R.layout.activity_starter);
             // rest of your code 
        }

About hiding the titlebar , you just need to set this theme on your Activity, in the manifest.xml.

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

And the action bar disappears.

I run your code (only the xml layout)and the image shows, but I used other values for the padding. Check if the values you are using for padding are not the responsible for this issue.

Edit :

Use this code to see the actual size of your image:

    ViewTreeObserver vto = mImageView.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
           int finalHeight = mImageView.getMeasuredHeight();
           int finalWidth = mImageView.getMeasuredWidth();
           System.out.println("Height: " + finalHeight + " Width: " + finalWidth);
            return true;
        }
    });

You can hide action bar by

 getActionBar().hide(); //if API >=11

or

 getSupportActionBar().hide(): //if API <11

Also, I don't see any issue with your layout. I could see the image in my nexus 5.

 In manifest file just add the bold line for hiding titlebar
 <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    **android:theme="@android:style/Theme.NoTitleBar"** >
    <activity
        android:name="com.example.abc.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    </application>
and for Image just use this code in xml
<ImageView
    android:id="@+id/starterPic"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />
and this code in java
ImageView mImageView = (ImageView) findViewById(R.id.starterPic); 
mImageView.setImageResource(R.drawable.ic_launcher);

You have forgot to set the orientation of the LinearLayout . Try adding this to the xml within <LinearLayout > :

android:orientation="vertical"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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