Java ImageView未以线性布局显示(android)
我正在创建一个android应用程序,在第一阶段就已经遇到了一些问题 我试图在布局中显示带有ImageView的图像 我的代码如下: activity_starter.xml:Java ImageView未以线性布局显示(android),java,android,android-layout,android-imageview,android-view,Java,Android,Android Layout,Android Imageview,Android View,我正在创建一个android应用程序,在第一阶段就已经遇到了一些问题 我试图在布局中显示带有ImageView的图像 我的代码如下: activity_starter.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_par
<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>
这些问题是:
我无法隐藏顶部黑条上的标题栏,标题栏上写着WhereIsIt应用程序。名称
图像视图中的图像未显示。
我已经深入研究了几个小时,我做错了什么?关于隐藏标题栏,您只需要在manifest.xml中为您的活动设置此主题
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
动作栏消失了
我只运行xml布局和图像显示的代码,但我使用了其他值作为填充。
检查用于填充的值是否不是导致此问题的原因
编辑:
使用以下代码查看图像的实际大小:
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;
}
});
您可以通过以下方式隐藏操作栏:
getActionBar().hide(); //if API >=11
或
另外,我认为你的布局没有任何问题。我可以在nexus 5中看到图像。您忘记设置线性布局的方向。 请尝试在以下范围内将其添加到xml: 要隐藏标题栏,只需将以下代码放入onCreate方法中:
事实上,现在我已经在menifest文件中设置了你给我的主题,我得到的不是空白的白色视图,而是空白的黑色视图。因此,现在我需要两个修复-使图像显示,背景颜色恢复为白色;@user1067083改用android:theme=@android:style/theme.Light.NoTitleBar.Fullscreen:Dhaha…好了,现在我回到白色空白视图…该图像有任何新提示吗?不要在onCreate中对图像进行任何更改。尝试将图像设置为LinearLayout的背景,并查看它是否出现。另外,尝试在布局宽度和布局高度上使用匹配父项,并删除布局重量。仍然相同。尽管我告诉你我注意到了什么——例如,如果我记录imageview的getHeight,在onCreate中的代码之后,我得到0。你认为它有什么意义吗?为什么你叫它mImageView.invalidate;?没有它它也无法显示,这很奇怪……我在我的星系s3中看不到它,而且它在屏幕大小方面也没有太大区别;它在S4中也可以正常工作:尝试删除布局的权重,并更改布局的宽度=包裹内容。不要在onCreate中生成任何代码。。只需使用我建议的所有上述更改来执行setContentView。您可以分享您在活动中所做的全部代码吗。您是否在“启动/恢复”活动中做任何事情?
getActionBar().hide(); //if API >=11
getSupportActionBar().hide(): //if API <11
android:orientation="vertical"
@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
}
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);