Android ScrollView中imageView布局的基本问题
我有一个包含一个imageView的ScrollView 我希望这个imageView与屏幕的父宽度、全宽相匹配,然后保持比例纵横比,这样就可以环绕高度 我的选择,其中: -布局\宽度:匹配\父级 -布局高度:包裹内容 -scaletype:中心裁剪 这是密码Android ScrollView中imageView布局的基本问题,android,android-layout,imageview,scrollview,Android,Android Layout,Imageview,Scrollview,我有一个包含一个imageView的ScrollView 我希望这个imageView与屏幕的父宽度、全宽相匹配,然后保持比例纵横比,这样就可以环绕高度 我的选择,其中: -布局\宽度:匹配\父级 -布局高度:包裹内容 -scaletype:中心裁剪 这是密码 <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/cellarWineImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/etiquette_unknown" />
</LinearLayout>
</ScrollView>
结果如下
如果我只是把一个固定高度348dp,结果在这里
战斗结束后,我怀疑包裹内容,考虑图像的原始高度,大小是否因匹配父的大小而改变并不重要。请为这些基本主题提供帮助。我希望我们可以在不使用任何代码的情况下处理此问题。
尝试使用线性布局、ImageView和ScrollView
您的android:layout\u height=“fill\u parent”
看看是否有效。试试这种方法,希望这能帮助您解决问题。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="@+id/cellarWineImageView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"
android:scaleType="fitXY" />
</LinearLayout>
像这样更改布局
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:minHeight="348dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/cellarWineImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:minHeight="348dp"
android:src="@drawable/ic_launcher" />
</LinearLayout>
基于这个很好的答案,我们可以使用简单的东西,将ImageView扩展到一个新组件:AspectRatioImageView,然后在布局中直接重用它
package com.yourpackage.widgets;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AspectRatioImageView extends ImageView {
public AspectRatioImageView(Context context) {
super(context);
}
public AspectRatioImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AspectRatioImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (getDrawable() == null)
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
else {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth();
setMeasuredDimension(width, height);
}
}
}
现在在XML中使用它:
<com.yourpackage.widgets.AspectRatioImageView android:layout_centerHorizontal="true"
android:src="@drawable/yourdrawable" android:id="@+id/image"
android:layout_alignParentTop="true" android:layout_height="wrap_content"
android:layout_width="match_parent" android:adjustViewBounds="true" />
这在我的项目中非常有效,只需通过这个新的packageName更改当前的ImageView即可。然后一切都很顺利 他可能应该在他的图像视图中删除“中心作物”也是啊,我没看到,但我想你是对的!我使用了match_parent(这是推荐的,我希望是相同的),看起来更好,但它是假的,比率方面没有保留。为了更好地检查,我在视图中放置了两个图像,一个接一个,一个被另一个隐藏。谢谢@Haresh,但是图像没有包装到其最大尺寸(屏幕宽度)。视图包裹宽度很好,但内部图像很小。高度没有真正意义(屏幕高度的2/5)你试试看,好吗谢谢,但这张图片是一个例子,我可以在运行时显示许多不同的大小我在布局中设置了android:src,但是getDrawable()返回null。当我检查null时,它还是崩溃了:java.lang.IllegalStateException:onMeasure()没有通过在开始时调用setMeasuredDimension()@Makalele来设置测量的维度,在设置或膨胀内容之前,可绘制的内容似乎为null,可能是正常的生命周期。因此,只要有可提取的,你就可以做这项工作。那对我来说很好。