如何在 Android 中的给定布局内调整 ImageView 的大小?
How to resize an ImageView within a given Layout in Android?
我无法针对特殊情况进行适当的布局。我已经在设计器和代码中对此进行了一段时间的试验,但我找不到解决方案,这就是为什么我需要你的帮助。
我必须创建一个布局,其结构应如下图所示。主要是几个linearLayout的组合。我遇到的问题是,图片只能在代码中添加,因为此布局是显示列表中项目信息的详细视图。
- 顶部是没有图像占位符的布局(未加载图片 - 以黑色表示),此处“linearLayout_BigLeft”的宽度由下式给出"linearLayout_BelowImage".
中两个按钮和textView(都有内容)的宽度
- 在中间,您可以看到在代码中加载图片(图片以橙色表示)后的布局。根据 android 设备的宽高比,黑色间隙会有所不同。我无法将图像调整为整个可用高度并相应地调整其宽度。 “linearLayout_BelowImage”根据图像大小自行调整(其中的 textView 越来越宽)。
- 下方是理想状态的布局图。图像始终应使用整个可用的高度 space 并相应地调整宽度。 “linearLayout_BelowImage”根据图像大小自行调整(其中的 textView 越来越宽)。
问题:
如何获得看起来像底部图片的布局(在代码中加载图像后)?图像在代码中加载后,必须自行调整大小,因此它使用整个可用高度并相应地调整其宽度。 "relativeLayout_Top" 和“linearLayout_BelowImage”都有固定的高度。 “scrollView_BigRight”根据 space 进行自我调整,而“imageView_OrangeImage”不会需要自己。
我可以处理在添加图像后调整代码布局的解决方案,或者使 layout.xml 本身足够灵活以处理这种情况的解决方案。
非常感谢任何帮助。如果您需要更多信息,请告诉我。
下面是我的layout.xml的主要内容,需要解决这个问题
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/white">
<RelativeLayout
android:id="@+id/relativeLayout_Top"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/blue" >
</RelativeLayout>
<LinearLayout
android:id="@+id/linearLayout_Big"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="@color/transparent" >
<LinearLayout
android:id="@+id/LinearLayout_BigLeft"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/transparent" >
<ImageView
android:id="@+id/imageView_OrangeImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/black" />
<LinearLayout
android:id="@+id/linearLayout_BelowImage"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/blue_white_blue" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/blue" />
<TextView
android:id="@+id/textView_BelowImageMiddle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/white" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/blue" />
</LinearLayout>
</LinearLayout>
<ScrollView
android:id="@+id/scrollView_BigRight"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/grey" >
</ScrollView>
</LinearLayout>
</LinearLayout>
android:adjustViewBounds="true"
This one’s a manual fix for “optimized” code in scaleType="fitCenter". Basically when Android adds an image resource to the ImageView it tends to get the width & height from the resource instead of the layout. This can cause layouts to reposition around the full size of the image instead of the actual viewable size.
AdjustViewBounds forces Android to resize the ImageView to match the resized image prior to laying everything else out. There are times where this calculation won’t work, such as when the ImageView is set to layout_width="0dip". If it’s not working, wrap the ImageView in a RelativeLayout or FrameLayout which handles the 0dip flexible size instead
或
模式android:scaleType="centerCrop"
均匀拉伸图像以填充整个容器并进行不必要的修剪。
You can change the way it default scales images using the android:scaleType parameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself!
get it here
我无法针对特殊情况进行适当的布局。我已经在设计器和代码中对此进行了一段时间的试验,但我找不到解决方案,这就是为什么我需要你的帮助。
我必须创建一个布局,其结构应如下图所示。主要是几个linearLayout的组合。我遇到的问题是,图片只能在代码中添加,因为此布局是显示列表中项目信息的详细视图。
- 顶部是没有图像占位符的布局(未加载图片 - 以黑色表示),此处“linearLayout_BigLeft”的宽度由下式给出"linearLayout_BelowImage". 中两个按钮和textView(都有内容)的宽度
- 在中间,您可以看到在代码中加载图片(图片以橙色表示)后的布局。根据 android 设备的宽高比,黑色间隙会有所不同。我无法将图像调整为整个可用高度并相应地调整其宽度。 “linearLayout_BelowImage”根据图像大小自行调整(其中的 textView 越来越宽)。
- 下方是理想状态的布局图。图像始终应使用整个可用的高度 space 并相应地调整宽度。 “linearLayout_BelowImage”根据图像大小自行调整(其中的 textView 越来越宽)。
问题: 如何获得看起来像底部图片的布局(在代码中加载图像后)?图像在代码中加载后,必须自行调整大小,因此它使用整个可用高度并相应地调整其宽度。 "relativeLayout_Top" 和“linearLayout_BelowImage”都有固定的高度。 “scrollView_BigRight”根据 space 进行自我调整,而“imageView_OrangeImage”不会需要自己。
我可以处理在添加图像后调整代码布局的解决方案,或者使 layout.xml 本身足够灵活以处理这种情况的解决方案。
非常感谢任何帮助。如果您需要更多信息,请告诉我。
下面是我的layout.xml的主要内容,需要解决这个问题
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/white">
<RelativeLayout
android:id="@+id/relativeLayout_Top"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/blue" >
</RelativeLayout>
<LinearLayout
android:id="@+id/linearLayout_Big"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="@color/transparent" >
<LinearLayout
android:id="@+id/LinearLayout_BigLeft"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/transparent" >
<ImageView
android:id="@+id/imageView_OrangeImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/black" />
<LinearLayout
android:id="@+id/linearLayout_BelowImage"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/blue_white_blue" >
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/blue" />
<TextView
android:id="@+id/textView_BelowImageMiddle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@color/white" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@color/blue" />
</LinearLayout>
</LinearLayout>
<ScrollView
android:id="@+id/scrollView_BigRight"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/grey" >
</ScrollView>
</LinearLayout>
</LinearLayout>
android:adjustViewBounds="true"
This one’s a manual fix for “optimized” code in scaleType="fitCenter". Basically when Android adds an image resource to the ImageView it tends to get the width & height from the resource instead of the layout. This can cause layouts to reposition around the full size of the image instead of the actual viewable size.
AdjustViewBounds forces Android to resize the ImageView to match the resized image prior to laying everything else out. There are times where this calculation won’t work, such as when the ImageView is set to layout_width="0dip". If it’s not working, wrap the ImageView in a RelativeLayout or FrameLayout which handles the 0dip flexible size instead
或
模式android:scaleType="centerCrop"
均匀拉伸图像以填充整个容器并进行不必要的修剪。
You can change the way it default scales images using the android:scaleType parameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself!
get it here