简体   繁体   English

Android layout_weight无法正常工作

[英]Android layout_weight not working as it should be

I'm trying to set my ImageView and LinearLayout to have half of the screen each. 我正在尝试将ImageView和LinearLayout设置为每个都有一半的屏幕。 This works fine on tablets, but on smaller screens the image takes up the entire screen (thus, not actually working). 这在平板电脑上可以正常工作,但在较小的屏幕上,图像会占据整个屏幕(因此,实际上无法正常工作)。

The way I see it is, I have set a layout_weight="1" on both the ImageView and LinearLayout and the layout_width="0dp" on both also. 我看到的方式是,我在ImageView和LinearLayout上都设置了layout_weight =“ 1”,也在两者上都设置了layout_width =“ 0dp”。 This SHOULD work, as I have researched a lot into this. 我应该对此进行大量研究,因此应该进行这项工作。

My Code: 我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/button_text_white">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:scrollbars="none">

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

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:background="@drawable/image"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:padding="20dp"
                android:orientation="vertical"
                android:gravity="bottom">

                <TextView
                    android:id="@+id/text_view_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="25dp"
                    android:gravity="center"
                    android:textSize="24dp"
                    android:textColor="@color/button_text_white"
                    android:visibility="invisible"/>

                <TextView
                    android:id="@+id/text_username"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/sign_up_in_username_text"
                    android:textColor="@color/color"/>

                <EditText
                    android:id="@+id/edit_text_username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="15dp"
                    android:inputType="text"
                    android:textColor="@color/color"
                    android:hint="@string/sign_up_in_username_hint"/>

                <TextView
                    android:id="@+id/text_email"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/sign_up_in_email_text"
                    android:textColor="@color/color"/>

                <EditText
                    android:id="@+id/edit_text_email"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="15dp"
                    android:inputType="textEmailAddress"
                    android:textColor="@color/color"
                    android:hint="@string/sign_up_in_email_hint"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/sign_up_in_password_text"
                    android:textColor="@color/color"/>

                <EditText
                    android:id="@+id/edit_text_password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="15dp"
                    android:inputType="textPassword"
                    android:textColor="@color/color"
                    android:hint="@string/sign_up_in_password_hint"/>

                <TextView
                    android:id="@+id/text_view_forgotten_password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="15dp"
                    android:gravity="right"
                    android:text="@string/forgotten_password_text"
                    android:textColor="@color/color"/>

                <Button
                    android:id="@+id/button_sign_up_in"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:textColor="@color/color"/>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

Thanks, appreciate any help! 谢谢,谢谢您的帮助!

I would say your problem is with your LinearLayout 's height being set to match_parent inside of a ScrollView . 我会说您的问题是在ScrollView中将LinearLayout的高度设置为match_parent

As you know, a ScrollView expands to fit its contents - now if you tell its contents to expand as big as its parent (match_parent), then you have a paradox. 如您所知,ScrollView会扩展以适合其内容-现在,如果您告诉它的内容扩展到与其父项(match_parent)一样大,那么就会有一个悖论。

Try setting the child of your ScrollView's height to wrap_content instead of match_parent . 尝试将ScrollView高度的子级设置为wrap_content而不是match_parent

There is no use for your ScrollView & it's parent RelativeLayout. ScrollView及其父级RelativeLayout没有用。 Cut down your layout as below. 缩小布局,如下所示。

<LinearLayout>
    <ImageView w="1"/>
    <ScrollView w="1">
        <LinearLayout>
            ....
        </LinearLayout>
    </ScrollView>
</LinearLayout>

.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/button_text_white">

   <ImageView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:background="@drawable/image"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="20dp"
            android:orientation="vertical"
            android:gravity="bottom">

            <TextView
                android:id="@+id/text_view_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="25dp"
                android:gravity="center"
                android:textSize="24dp"
                android:textColor="@color/button_text_white"
                android:visibility="invisible"/>

            <TextView
                android:id="@+id/text_username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/sign_up_in_username_text"
                android:textColor="@color/color"/>

            <EditText
                android:id="@+id/edit_text_username"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:inputType="text"
                android:textColor="@color/color"
                android:hint="@string/sign_up_in_username_hint"/>

            <TextView
                android:id="@+id/text_email"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/sign_up_in_email_text"
                android:textColor="@color/color"/>

            <EditText
                android:id="@+id/edit_text_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:inputType="textEmailAddress"
                android:textColor="@color/color"
                android:hint="@string/sign_up_in_email_hint"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/sign_up_in_password_text"
                android:textColor="@color/color"/>

            <EditText
                android:id="@+id/edit_text_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:inputType="textPassword"
                android:textColor="@color/color"
                android:hint="@string/sign_up_in_password_hint"/>

            <TextView
                android:id="@+id/text_view_forgotten_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="15dp"
                android:gravity="right"
                android:text="@string/forgotten_password_text"
                android:textColor="@color/color"/>

            <Button
                android:id="@+id/button_sign_up_in"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:textColor="@color/color"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM