如何根据Android布局中的layout_width指定layout_height值?

时间:2016-06-12 18:22:58

标签: java android android-layout android-imageview android-styles

我在Android开发方面绝对是新手,我在第一个正在开发的应用中定位图像时遇到了问题。

所以我有以下情况。

进入布局文件我有:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Dummy content. -->
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="0dp">

       <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="300dp"
            android:src="@drawable/carbonara" />

        <TextView android:id="@android:id/text1"
            style="?android:textAppearanceLarge"
            android:textStyle="bold"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp" />

    </LinearLayout>

</ScrollView>

因此,正如您所看到的,有 ImageView 元素:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="300dp"
    android:src="@drawable/carbonara" />

正如您所看到的,对于 ImageView ,我为宽度和高度设置了以下值

android:layout_width="wrap_content"
android:layout_height="300dp

这是因为我希望图像水平占据所有空间,但我还要为高度设置一个vaue。

因此,对于之前的值,我得到以下结果:

enter image description here

正如您在上一个屏幕截图中看到的那样,图片上下有一个空格,因为我手动为我的 ImageView 元素提供了一个特定值,这是错误的。

我尝试设置较低的hwight值,并为 android:layout_height =&#34; 240dp&#34; 值删除此空白区域。

但我认为这不是一个好的解决方案,因为它取决于使用过的屏幕。

那么,处理这种情况的最佳方法是什么?

我有一个水平必须填满屏幕的图像视图,它的高度已经自动处理,而不是用dp值手动指定。

如何实现此行为?我错过了什么?

3 个答案:

答案 0 :(得分:1)

如果您需要重新缩放图片

,请尝试使用android:scaleType="centerCrop"android:scaleType="fitXY"

答案 1 :(得分:1)

朋友我完全理解你的问题我也对这些烦人的东西感到困惑,但现在我学到了许多真正有用的东西.. 所以你可以通过以下简单的代码解决你的问题

这个的XML属性是......

机器人:scaleType = “fitCenter”

把它放在这里,你的代码如下所示,我刚添加新行,请参阅#1

<div class="form-group">
                            <label for="password">Password</label>
                            <input type="password" name="password" class="form-control">
                          {{json_encode($errors)}}
                            @foreach ($errors->get('password') as $error)
                                <p class="checkbox help-block">
                                    <small>{{$error}}</small>
                                </p>
                            @endforeach
                        </div>

我希望我已经解释得很好。如果它帮助你做出了这个答案。 你也可以使用src但你可以使用background属性            机器人:背景= “@绘制/培根蛋面”

答案 2 :(得分:1)

您可以使用屏幕宽高比设置图像的高度和宽度。请尝试以下功能 public void calculateAspectRatio(){

int imagewidth;
int imagewidth;
DisplayMetrics displaymetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);  
             int height = displaymetrics.heightPixels;
            int width = displaymetrics.widthPixels;

             imagewidth=width;
            imageHeight = (imageTwoWidth / 4) * 3;
}

set image height and width

            imageview.requestLayout();
            imageview.getLayoutParams().width = imagewith;//take value from above function for height and width.
            imageview.getLayoutParams().height =imageheight;

`