尽管adjustViewBounds

时间:2017-05-14 14:29:16

标签: android android-layout android-imageview android-percentrelativelayout

我想在ImageView中包含LinearLayout,以便我可以将一组视图居中。但是,原始图片需要按比例缩小以适合ImageView,并且原始尺寸会扩展LinearLayout,尽管我使用了adjustViewBounds="true"和封闭的FrameLayoutprevious questions在SO上建议。

所需的布局应如this

但观察到的布局看起来像this

由以下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"的其他suggestion,因为我需要相对于屏幕的高度。

3 个答案:

答案 0 :(得分:0)

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

您可以将其与android:maxWidth="60dp"

结合使用

所以你的imageView应该是这样的。

            <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 :(得分:0)

你可以做的事情:

1)为包含ImageView的FrameLayout设置特定的宽度/高度,并为ImageViwe设置android:scaleType到centerInside,fitCenter或fitXY。

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;
    }
}

注意: 宽度以像素表示。 要获取显示指标: How to get screen display metrics in application class

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

答案 2 :(得分:0)

根据this对其他问题的回答,删除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的answerthese说明,以下代码也可以达到预期效果:

@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);
}