ImageView 扩展容器,尽管 adjustViewBounds



我想将ImageView包装在LinearLayout中,以便我可以将一组视图居中。然而,原始图像需要缩小以适应ImageView,并且原始大小扩展了LinearLayout,尽管我按照之前关于SO的问题的建议使用了adjustViewBounds="true"和封闭FrameLayout

所需的布局应如下所示,

但是观察到的布局看起来像这样,

由以下 XML 生成:

<android.support.percent.PercentRelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="project.MainActivity">
        <LinearLayout
            android:layout_width="wrap_content"
            app:layout_heightPercent="32%"
            android:orientation="horizontal"
            android:background="#b44343"
            android:layout_centerHorizontal="true"
            android:layout_alignParentTop="true">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sample Text"/>
            <FrameLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#5555ae">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="#2c8c4c"
                    android:src="@drawable/spades_icon"
                    android:adjustViewBounds="true"
                    android:scaleType="centerInside"/>
            </FrameLayout>
        </LinearLayout>
    </android.support.percent.PercentRelativeLayout>

我不能使用设置android:maxHeight="100dp"的其他建议,因为我需要高度相对于屏幕的高度。

我看到您添加了android:adjustViewBounds="true".

您可以将其与android:maxWidth="60dp"结合使用

所以你的图像视图应该看起来像这样。

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="#2c8c4c"
                android:src="@drawable/spades_icon"
                android:adjustViewBounds="true"
                android:maxWidth="60dp"
                android:scaleType="centerInside"/>

您可以将最大宽度更改为所需的任何数字。

你可以

做的事情:

1( 为包含 ImageView 的 FrameLayout 设置特定的宽度/高度,并将 android:scaleType 设置为 CenterInside、fitCenter 或 fitXY for ImageViwe。

2(以编程方式,在您的活动中,在onCreate之后,例如在onResume中,您可以获取LayoutParams并更改ImageView的宽度和高度,从而进行自己的缩放。当我在运行时缩放屏幕宽度或高度时,我会接受这个赞美。

编辑 1

第二种选择的示例:

public class TestActivity extends AppCompatActivity {
    ImageView imgView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testactivity_layout);
        imgView = (ImageView) findViewById(R.id.imgview);
    }
    @Override
    protected void onResume() {
        super.onResume();
        ViewGroup.LayoutParams params = imgView.getLayoutParams();
        params.width = 100;
    }
}

笔记:宽度以像素表示。要获取显示指标,请执行以下操作:如何获取应用程序类中的屏幕显示指标

要为 ImageView 建立相对宽度,请获取显示的宽度并计算所需的 % 作为图像的宽度。

基于对另一个问题的回答,在保留图像高度和纵横比的同时删除LinearLayout中的空格的解决方案是:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    LinearLayout layout = (LinearLayout) findViewById(R.id.mLinearLayout);
    ImageView imageView = (ImageView) findViewById(R.id.mImageView);
    TextView textView = (TextView) findViewById(R.id.mTextView);
    layout.getLayoutParams().width = textView.getWidth()+imageView.getWidth();
    layout.requestLayout();
}

编辑:
根据@Juan的回答和这些说明,以下代码也达到了预期的结果:

@Override
protected void onResume() {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    this.getWindowManager()
        .getDefaultDisplay()
        .getMetrics(displayMetrics);
    ViewGroup.LayoutParams params = imgView.getLayoutParams();
    params.height = (int)Math.floor(displayMetrics.heightPixels * 0.32);
}

相关内容

最新更新