【问题标题】:Layout changes when dynamic activity is started动态活动启动时布局发生变化
【发布时间】:2018-01-02 11:27:11
【问题描述】:

我有一个允许创建动态 EditText 的布局 - 这工作正常,但它推高了我不想要的静态 EditTexts。

我使用的是滚动视图,所以理论上静态 EditText 应该锚定在适当的位置,当添加动态 EditTexts 时,用户可以向下滚动。

相反,随着更多动态 EditText 的添加,我开始使用的 3 个静态 EditText 越来越靠近。动态 EditTexts 中的间距很好,因为它们都是同一个数组的一部分,但我似乎看不到静态 EditTexts 发生了什么。

任何帮助将不胜感激。

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/facebookBlue"
        android:orientation="vertical"
        android:weightSum="1"
        tools:context="com.test.practise.AddTeamMembers">


        <android.support.design.widget.TextInputEditText
            android:id="@+id/tv_teamNames"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/teamName"
            android:textColor="@android:color/background_light"
            android:textColorLink="@android:color/background_light"
            android:textSize="30sp"
            android:textStyle="bold" />


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.26"
            android:orientation="vertical"
            android:weightSum="1">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="0.47"
                android:gravity="center"
                android:text="Enter Player Names Below!"
                android:textColor="@android:color/background_light"
                android:textSize="24sp" />

        </FrameLayout >


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.10"
            android:orientation="vertical"
            android:weightSum="1">

            <EditText

                android:id="@+id/et_team_name1"
                android:layout_width="232dp"
                android:layout_height="37dp"
                android:layout_gravity="center"
                android:layout_marginBottom="0dp"
                android:layout_weight="1"
                android:background="@android:color/background_light"
                android:ems="10"
                android:hint="@string/PlayerName"
                android:imeOptions="actionDone"
                android:inputType="text"
                android:paddingLeft="90dp"
                tools:layout_editor_absoluteX="76dp"
                tools:layout_editor_absoluteY="188dp"
                />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.10"
            android:orientation="vertical"
            android:weightSum="1">

            <EditText
                android:id="@+id/et_team_name2"
                android:layout_width="232dp"
                android:layout_height="37dp"
                android:layout_gravity="center"
                android:layout_marginBottom="0dp"
                android:layout_weight="1"
                android:background="@android:color/background_light"
                android:ems="10"
                android:hint="@string/PlayerName"
                android:imeOptions="actionDone"
                android:inputType="text"
                android:paddingLeft="90dp"
                android:singleLine="true"
                tools:layout_editor_absoluteX="76dp"
                tools:layout_editor_absoluteY="188dp"
                />

        </FrameLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dp"
            android:layout_weight="0.10"
            android:orientation="vertical"
            android:weightSum="1">

            <EditText
                android:id="@+id/et_team_name3"
                android:layout_width="232dp"
                android:layout_height="37dp"
                android:layout_gravity="center"
                android:layout_marginBottom="0dp"
                android:background="@android:color/background_light"
                android:ems="10"
                android:hint="@string/PlayerName"
                android:imeOptions="actionDone"
                android:inputType="text"
                android:paddingLeft="90dp"
                android:singleLine="true"
                tools:layout_editor_absoluteX="76dp"
                tools:layout_editor_absoluteY="188dp"
                android:layout_weight="1"
                />

        </FrameLayout>

        <LinearLayout
            android:id="@+id/editTextGroupLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical"></LinearLayout>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.07"
            android:orientation="vertical"
            android:weightSum="1">

            <TextView
                android:id="@+id/tv_add_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:text="+ Add Name"
                android:textColor="@android:color/background_light"
                android:textSize="16dp" />

        </FrameLayout >


        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1">

            <Button
                android:id="@+id/btn_submit_team"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@color/facebookBlue"
                android:gravity="center"
                android:text="Ready to join!"
                android:textColor="@android:color/background_light" />

        </FrameLayout >


    </LinearLayout>

</ScrollView>

结果是这样的

【问题讨论】:

  • 为什么要将每个小部件都包装在 FrameLayout 中?这似乎没用。 android:orientation="vertical" 对 FrameLayout 没有任何作用。此外,您的布局定义中有一个死的 LinearLayout。尝试从父级 LinearLayout 和任何子级中删除 android:weightSum="1",您似乎没有正确使用权重。
  • 啊,是的,当我使用线性布局时,方向被错误地留在了那里 - 只是在做实验。至于'dead LinearLayout',这在我的代码中被引用为动态EditText的一部分。

标签: android xml android-layout dynamic


【解决方案1】:

如果没有看到您的所有代码,很难说,但我相信您的问题源于在您的 ScrollView 上使用 android:fillViewport 和在您的 LinearLayout 的孩子上使用 android:layout_weight

您的ScrollView 的孩子LinearLayout 的身高为wrap_content。但是,由于ScrollView 上的fillViewport 属性,您的LinearLayout 将被拉伸以填满屏幕。这意味着会有一些“额外”的高度。

layout_weight 属性将额外的高度分开,并将其中的一部分分配给每个孩子。

当您将EditTexts 动态添加到视图中时,“额外”的高度量会减少(直到没有额外的高度)。这具有将原始视图越来越靠近的视觉效果。

解决方案将完全取决于您想要实现的目标。我怀疑答案是您应该从视图中删除 layout_weight 属性,而是使用边距在视觉上分隔它们。

【讨论】:

  • 是的,是 layout_weight 属性导致了问题,现在一切正常,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-13
  • 1970-01-01
  • 2013-01-27
  • 2013-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多