简体   繁体   English

ImageView不以线性布局显示(android)

[英]ImageView is NOT SHOWING in a linear layout (android)

I'm creating an android application and got into issues already in the first stage. 我正在创建一个android应用程序,并且已经在第一阶段遇到了问题。

I'm trying to display an image with an ImageView inside a Layout. 我正在尝试在Layout中显示带有ImageView的图像。

My code is as follows: 我的代码如下:

activity_starter.xml: 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: 和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)). 我无法在顶部隐藏标题栏(黑色栏显示“ WhereIsIt”(应用名称))。
  2. The image in the imageview is not shown. imageview中的图像未显示。

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 : 要隐藏Title bar ,只需将以下代码放在onCreate方法中:

    @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. 关于隐藏titlebar ,您只需在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. 我运行您的代码(仅xml布局),并显示图像,但是我使用了其他值作为填充。 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. 我可以在连结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 . 您忘记了设置LinearLayout的方向。 Try adding this to the xml within <LinearLayout > : 尝试将其添加到<LinearLayout >的xml中:

android:orientation="vertical"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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