背景图像拉伸太多

时间:2018-03-17 10:12:05

标签: android

我正在构建一个Android应用程序。在首页我显示一个随机的背景图像。这意味着宽度和高度是动态的。问题是背景图像正在被拉伸。

我在LinearLayout中使用代码

显示它
android:background="@drawable/img_salad_thumb".

以下是应用的截图:

This is how my app looks

正如您所看到的,徽标背后的背景图像非常紧张......

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true"
    android:fillViewport="true">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayoutMain"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/img_salad_thumb"
        android:scaleType="fitCenter">

        <!-- Header -->
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="1">

            <TableRow
                android:id="@+id/tableRow1">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="40dp"
                    android:layout_marginLeft="28dp"
                    android:orientation="vertical">

                    <!-- Category -->
                    <ImageView
                        android:id="@+id/imageViewCategory"
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        app:srcCompat="@drawable/ic_menu_white_24px"/>

                    <!-- Favorite -->
                    <ImageView
                        android:id="@+id/imageViewFavorite"
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:layout_marginTop="12dp"
                        app:srcCompat="@drawable/ic_favorite_white_24px" />

                </LinearLayout>

                <!-- Logo -->
                <ImageView
                    android:id="@+id/imageViewLogoSite"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal|center"
                    app:srcCompat="@drawable/img_circle_96"
                    android:layout_marginTop="24dp"
                    android:layout_marginBottom="1dp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="40dp"
                    android:layout_marginRight="28dp"
                    android:orientation="vertical">

                    <!-- Home -->
                    <ImageView
                        android:id="@+id/imageViewWebsite"
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        app:srcCompat="@drawable/ic_home_white_24px"/>

                    <!-- Comment -->
                    <ImageView
                        android:id="@+id/imageViewDiscuss"
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:layout_marginTop="12dp"
                        app:srcCompat="@drawable/ic_question_answer_white_24px"/>
                </LinearLayout>

            </TableRow>

        </TableLayout>

        <TextView
            android:id="@+id/textViewLogoTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:shadowColor="#000"
            android:shadowDx="3"
            android:shadowDy="3"
            android:shadowRadius="6"
            android:text="@string/app_name"
            android:textSize="28sp"
            android:textColor="@color/colorWhite"
            android:layout_marginBottom="12dp"  />
        <!-- //Header -->


        <!-- Search -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical">

            <EditText
                android:id="@+id/editTextSearchQuery"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/ic_search_grey_24px"
                android:background="@drawable/edittext_activity_main_search"
                android:hint="@string/search_for_recipe"
                android:inputType="text"
                android:singleLine="false"
                android:text=""
                android:layout_weight=".60"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:layout_marginBottom="16dp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="6dp"
                android:background="@android:color/background_light"/>

        </LinearLayout>
        <!-- //Search -->

        <!-- Recipes -->
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@color/colorLightGray">

            <ListView
                android:id="@+id/listViewRecipes"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:dividerHeight="1dp"></ListView>

        </LinearLayout>
        <!-- //Recipes -->




    </LinearLayout>
</ScrollView>

2 个答案:

答案 0 :(得分:3)

因为在主线性布局中设置图像在layout_width和layout_height中都有math_parent ...实际上你的图像填满了背景中的所有屏幕,你只看到它的一部分。

像这样更新您的代码

enter code her<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:fitsSystemWindows="true">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayoutMain"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/img_salad_thumb"
        android:orientation="vertical"
        android:scaleType="fitCenter">

        <!-- Header -->
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:stretchColumns="1">

            <TableRow android:id="@+id/tableRow1">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginLeft="28dp"
                    android:layout_marginTop="40dp"
                    android:orientation="vertical">

                    <!-- Category -->
                    <ImageView
                        android:id="@+id/imageViewCategory"
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        app:srcCompat="@drawable/ic_menu" />

                    <!-- Favorite -->
                    <ImageView
                        android:id="@+id/imageViewFavorite"
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:layout_marginTop="12dp"
                        app:srcCompat="@drawable/ic_call" />

                </LinearLayout>

                <!-- Logo -->
                <ImageView
                    android:id="@+id/imageViewLogoSite"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="1dp"
                    android:layout_marginTop="24dp"
                    android:gravity="center_horizontal|center"
                    app:srcCompat="@drawable/img_circle_96" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginRight="28dp"
                    android:layout_marginTop="40dp"
                    android:orientation="vertical">

                    <!-- Home -->
                    <ImageView
                        android:id="@+id/imageViewWebsite"
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        app:srcCompat="@drawable/ic_home_white_24px" />

                    <!-- Comment -->
                    <ImageView
                        android:id="@+id/imageViewDiscuss"
                        android:layout_width="36dp"
                        android:layout_height="36dp"
                        android:layout_marginTop="12dp"
                        app:srcCompat="@drawable/ic_question_answer_white_24px" />
                </LinearLayout>

            </TableRow>

        </TableLayout>

        <TextView
            android:id="@+id/textViewLogoTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="12dp"
            android:gravity="center"
            android:shadowColor="#000"
            android:shadowDx="3"
            android:shadowDy="3"
            android:shadowRadius="6"
            android:text="@string/app_name"
            android:textColor="@color/colorWhite"
            android:textSize="28sp" />
        <!-- //Header -->


        <!-- Search -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:orientation="vertical">

            <EditText
                android:id="@+id/editTextSearchQuery"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:layout_marginLeft="12dp"
                android:layout_marginRight="12dp"
                android:layout_weight=".60"
                android:background="@drawable/edittext_activity_main_search"
                android:drawableLeft="@drawable/ic_search_grey_24px"
                android:hint="@string/search_for_recipe"
                android:inputType="text"
                android:singleLine="false"
                android:text="" />

            <View
                android:layout_width="match_parent"
                android:layout_height="6dp"
                android:background="@android:color/background_light" />

        </LinearLayout>
        <!-- //Search -->

    </LinearLayout>


    <!-- Recipes -->
    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorLightGray"
        android:orientation="vertical">

        <ListView
            android:id="@+id/listViewRecipes"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:dividerHeight="1dp"></ListView>

    </LinearLayout>
    <!-- //Recipes -->


</LinearLayout>

答案 1 :(得分:-1)

嗨,在ImageView中设置属性'android:scaleType'可能对您有帮助。

您可以在此处获取有关'scaleType'的更多信息

ImageView.ScaleType | Android Developers