nestedScrollView正在android中展开imageview

时间:2016-04-07 08:27:13

标签: android imageview

我正在尝试将一个imageview嵌套在一个nestedscrollview内的linearlayout中。但是nestedscrollview正在拉伸imageview,我似乎无法找到错误:

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:fitsSystemWindows="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/llImageview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="?attr/actionBarSize"
                android:layout_weight="0.6"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/ivNewProductBarcode"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:adjustViewBounds="true"
                    android:background="@drawable/no_barcode_image"
                    android:scaleType="centerCrop"/>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.45"
                android:background="@color/cardview_dark_background"
                android:baselineAligned="true"
                android:orientation="vertical">


                <LinearLayout
                    android:id="@+id/llRest"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/ivDateIconView"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_toStartOf="@+id/etProductName"
                        android:background="@color/card_background"
                        android:contentDescription="ImageView for icon: Date" />

                    <EditText
                        android:id="@+id/etProductName"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="40dp"
                        android:layout_marginRight="30dp"
                        android:layout_marginTop="30dp"
                        android:hint="Name" />

                    <EditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="40dp"
                        android:layout_marginRight="30dp"
                        android:layout_marginTop="80dp"
                        android:hint="Type" />
                </LinearLayout>

            </LinearLayout>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_camera_alt_24dp"
        app:layout_anchor="@id/llImageview"
        app:layout_anchorGravity="bottom|right|end" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar_new_product"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        android:title="@string/newproducttitle"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


</android.support.design.widget.CoordinatorLayout>

以及它在手机上的外观图像: Image Stretched Image Stretched and rest of gui

2 个答案:

答案 0 :(得分:0)

试试这个

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/llImageview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="?attr/actionBarSize"
            android:layout_weight="0.6"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/ivNewProductBarcode"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:background="@mipmap/ic_launcher"
                android:scaleType="fitXY"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.45"
            android:background="@color/colorAccent"
            android:baselineAligned="true"
            android:orientation="vertical">


            <LinearLayout
                android:id="@+id/llRest"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <ImageView
                    android:id="@+id/ivDateIconView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_toStartOf="@+id/etProductName"
                    android:background="@color/colorAccent"
                    android:contentDescription="ImageView for icon: Date" />

                <EditText
                    android:id="@+id/etProductName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="40dp"
                    android:layout_marginRight="30dp"
                    android:layout_marginTop="30dp"
                    android:hint="Name" />

                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="40dp"
                    android:layout_marginRight="30dp"
                    android:layout_marginTop="80dp"
                    android:hint="Type" />
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:clickable="true"
    android:src="@mipmap/ic_launcher"
    app:layout_anchor="@id/llImageview"
    app:layout_anchorGravity="bottom|right|end" />

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar_new_product"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:title="product"
    app:layout_scrollFlags="scroll|enterAlways"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

答案 1 :(得分:0)

已修复:

我最终重新编码了整个布局文件,并更新了我的Gradle模块文件,因此设计库现在在23.3.0中。这以某种方式解决了问题。对于遇到同样问题的其他人,这是我的布局代码和Gradle脚本:

布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coAddNewProductContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/llImageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.6">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:adjustViewBounds="true"
                    android:src="@drawable/no_barcode_image" />
            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.4"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tvDatePick"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="50dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="60dp"
                    android:text="Date to pick" />

                <EditText
                    android:id="@+id/etName"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/tvDatePick"
                    android:layout_marginLeft="50dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="30dp"
                    android:hint="@string/testingString" />

                <EditText
                    android:id="@+id/etType"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@id/etName"
                    android:layout_marginLeft="50dp"
                    android:layout_marginRight="20dp"
                    android:layout_marginTop="30dp"
                    android:hint="@string/app_name" />

                <ImageView
                    android:layout_width="12dp"
                    android:layout_height="12dp"
                    android:layout_toLeftOf="@+id/etName" />

            </RelativeLayout>
        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_camera_alt_24dp"
        app:layout_anchor="@id/llImageView"
        app:layout_anchorGravity="bottom|right|end" />

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar_new_product"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed" />
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

Gradle:

...
 dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

    compile 'com.android.support:appcompat-v7:23.3.0'
    compile 'com.android.support:design:23.3.0'
    compile 'com.android.support:cardview-v7:23.3.0'
    compile 'com.android.volley:volley:1.0.0'
    compile 'com.google.android.gms:play-services:8.4.0'
    compile 'com.google.android.gms:play-services-ads:8.4.0'
    compile 'com.google.android.gms:play-services-identity:8.4.0'
    compile 'com.google.android.gms:play-services-gcm:8.4.0'
}
...