如何使ImageView尽可能大

时间:2016-05-18 08:24:51

标签: android xml android-layout android-studio android-cardview

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
                                    xmlns:app="http://schemas.android.com/apk/res-auto"
                                    android:layout_width="match_parent"
                                    android:layout_height="110dp"
                                    android:layout_margin="@dimen/news_item_cardview_margin"
                                    app:cardElevation="@dimen/cardview_default_elevation">

    <RelativeLayout
        android:id="@+id/relative_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp">

        <ImageView
            android:id="@+id/news_image1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:contentDescription="@string/news_image_description"/>

        <TextView
            android:id="@+id/news_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"
            android:layout_toEndOf="@id/news_image1"
            android:layout_toRightOf="@id/news_image1"
            android:textAppearance="?android:attr/textAppearanceMedium"/>


        <TextView
            android:id="@+id/news_pub_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/news_title"
            android:layout_alignParentBottom="true"
            android:layout_alignStart="@id/news_title"
            android:layout_toEndOf="@id/news_image1"
            android:layout_toRightOf="@id/news_image1"/>

        <TextView
            android:id="@+id/news_comments_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="8dp"
            android:drawableLeft="@drawable/ic_comment_black_18dp"
            android:drawableStart="@drawable/ic_comment_black_18dp"
            />
    </RelativeLayout>
</android.support.v7.widget.CardView>

Layout I want to create

我想创建一个像上面的布局xml的图像1一样的布局。但是当我测试它时,我发现图像视图不够大。它可能更高更广。

layout in emulator

我怎么能让imageview比textview更优先。

5 个答案:

答案 0 :(得分:1)

试试这个!!

添加

android:scaleType="centerCrop" 

到ImageView

<ImageView
        android:id="@+id/news_image1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:contentDescription="@string/news_image_description"/>

答案 1 :(得分:1)

检查下面的布局文件,其中从RelativeLayout更改为LinearLayout并使用 weightSum

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:cardElevation="@dimen/cardview_default_elevation" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="5dip"
        android:weightSum="3" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="5dip"
            android:layout_weight="1"
            android:padding="10dp" >

            <ImageView
                android:id="@+id/news_image1"
                android:layout_width="100dip"
                android:layout_height="100dip"
                android:contentDescription="news_image_description"
                android:src="@drawable/cal" />
        </LinearLayout >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_margin="5dip"
            android:layout_weight="2"
            android:orientation="vertical"
            android:weightSum="5" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="3" >

                <TextView
                    android:id="@+id/news_title"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="4"
                    android:gravity="left"
                    android:hint="Title"
                    android:textSize="24sp" />
            </LinearLayout >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_margin="5dip"
                android:layout_weight="2"
                android:gravity="bottom"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/news_pub_date"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.8"
                    android:gravity="left|bottom"
                    android:hint="Date"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/news_comments_count"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1.2"
                    android:gravity="right|bottom"
                    android:hint="Count"
                    android:textSize="18sp" />
            </LinearLayout >
        </LinearLayout >
    </LinearLayout >
</android.support.v7.widget.CardView >

答案 2 :(得分:0)

尝试在标记内使用weightweightSum属性。 例如。, 机器人:layout_weight = “0.7”

请阅读此问题:Android relative layout with button width using weight

答案 3 :(得分:0)

将其粘贴到您的CardView

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="150dp"
android:weightSum="3"
android:padding="16dp">

<ImageView
    android:id="@+id/news_image1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimaryDark"
    android:layout_weight="2"
    />
<LinearLayout
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:weightSum="4"
    android:background="#ffffff"
    android:layout_weight="1">
    <TextView
        android:id="@+id/news_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Title"
        android:layout_weight="3"
        android:textAppearance="?android:attr/textAppearanceMedium"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"

        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/news_pub_date"
            android:layout_width="wrap_content"
        android:layout_weight="1"
            android:text="Some"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/news_comments_count"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:gravity="end|right"
            android:layout_height="wrap_content"
            android:text="Count"
            />
    </LinearLayout>
</LinearLayout>

这是我得到的输出 Output

答案 4 :(得分:0)

我可以建议以编程方式为您解决问题。您必须为ImageView计算最佳比例,而不必更改原始图像的宽高比。

private void fitImageView(View parentView, ImageView imageView) {
    double maxHeight = parentView.getLayoutParams().height - parentView.getPaddingTop() - parentView.getPaddingBottom();
    Drawable drawable = imageView.getDrawable();

    double width = drawable.getMinimumWidth();
    double height = drawable.getMinimumHeight();
    double aspect = (float) width / height;

    width = maxHeight * aspect;
    height = maxHeight;

    imageView.getLayoutParams().height = (int) height;
    imageView.getLayoutParams().width = (int) width;
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}