需要有关 ImageView 始终显示不正确的帮助
由于某些奇怪的原因,我的 ImageView 总是在屏幕的中间、左侧显示图片,尽管 xml 代码坚持将其显示在中间。
我在我的应用程序中强制纵向,这是我的布局的 xml 代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/image_view"
android:layout_weight="1.0"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/message"
android:gravity="center_horizontal"
android:padding="10dip"/>
<Button
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"
android:textStyle="bold" android:id="@+id/action_button" android:selectAllOnFocus="false" android:layout_height="100dip" android:text="Click here to preform actions"/>
</LinearLayout>
因此,页面应显示为中间的图片,扩展屏幕的宽度,下面是文本,下面是按钮。但由于某种原因,图片在屏幕左中部显示为一个小的方框缩略图 - 关于解决方法有什么想法吗?
My ImageView for some odd reason is always displaying a picture in the middle, left of my screen despite the xml code insisting it to be in the center.
I force portrait orientation in my app and here is my xml code for my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/image_view"
android:layout_weight="1.0"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/message"
android:gravity="center_horizontal"
android:padding="10dip"/>
<Button
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"
android:textStyle="bold" android:id="@+id/action_button" android:selectAllOnFocus="false" android:layout_height="100dip" android:text="Click here to preform actions"/>
</LinearLayout>
So, the page should display as Picture in the middle, extending the width of the screen, the text below that and the button below that. But for some reason, the picture is displayed as a small, box thumbnail in the middle left of the screen - any ideas on a workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您希望您的图片位于顶部框中的中心,您不需要
android:gravity="center"
,您实际上只需要android:scaleType
在ImageView
本身上设置。如果您不想缩放,“center
”是正确的值;如果您这样做,centerInside
可能是合适的(在这种情况下,您需要在ImageView
上定义某种尺寸或重量)。有关scaleType值的更多信息,请参阅文档。您也不希望将图像高度设置为 fill_parent,否则它将执行此操作(这意味着其下方没有文本,因为图像填充了整个父
LinearLayout
,没有留下文本空间)。您可能希望将ImageView
的高度设置为wrap_content
或某个固定高度(例如100dip
)。Assuming you want your picture centered inside a top box here, you don't want
android:gravity="center"
, you actually just wantandroid:scaleType
set on theImageView
itself. "center
" is the right value if you don't want scaling;centerInside
is probably appropriate if you do (in which case you'll need to define some sort of dimensions or weight on yourImageView
). For more on scaleType values, see the documentation.You also don't want the image height set to fill_parent, or it will do that (which means no text below it, since the image fills the entire parent
LinearLayout
leaving no room for text). You probably want theImageView
's height set towrap_content
or some fixed height (e.g.100dip
).