java - Android XML 设计(布局)使我的应用程序如此缓慢

标签 java android xml

我为我的应用程序做了一个设计,但它运行速度非常慢。我想要实现的结果是这样的:

enter image description here

到目前为止,我已经能够使用此 xml 实现此结果:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.asteam.unify.Mbajtje">

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:layout_marginRight="@dimen/general_margin"
    android:layout_marginLeft="@dimen/general_margin">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lendaText"
            android:layout_marginTop="@dimen/general_margin"
            android:text="Lënda:"/>

        <Spinner
            android:id="@+id/lendaMbajtje"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/lendaText"
            android:textSize="@dimen/font_size"
            android:background="@drawable/border_bottom"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/subjektiText"
            android:layout_marginTop="@dimen/general_margin"
            android:layout_below="@id/lendaMbajtje"
            android:text="Subjekti:"/>

        <Spinner
            android:id="@+id/subjektiMbajtje"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/subjektiText"
            android:textSize="@dimen/font_size"
            android:background="@drawable/border_bottom"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/subjektiMbajtje"
            android:id="@+id/detajetMbajtjesText"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginRight="@dimen/general_margin"
                android:layout_weight="3">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:layout_weight="1"
                    android:text="Data e mbajtjes:"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:id="@+id/dates"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="3dp"
                    android:orientation="horizontal">

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/dayMbajtje"
                        android:background="@drawable/border_bottom"
                        android:layout_weight="1"
                        android:textSize="@dimen/font_size"
                        android:gravity="center"
                        android:layout_marginRight="5dp"
                        android:text="12"
                        android:paddingBottom="@dimen/padding_spiner"
                        android:singleLine="true"
                        android:inputType="number"
                        android:imeOptions="actionNext"/>

                    <Spinner
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/monthMbajtje"
                        android:background="@drawable/border_bottom"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:textSize="@dimen/font_size"
                        android:layout_marginRight="5dp"
                        android:text="02"/>

                    <Spinner
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/yearMbajtje"
                        android:background="@drawable/border_bottom"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:textSize="@dimen/font_size"
                        android:text="2016"/>

                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginLeft="@dimen/general_margin"
                android:layout_weight="2">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:layout_weight="1"
                    android:text="Ora e mbajtjes:"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:layout_marginTop="3dp"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/hourMbajtje"
                        android:background="@drawable/border_bottom"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:textSize="@dimen/font_size"
                        android:text="22"
                        android:paddingBottom="@dimen/padding_spiner"
                        android:singleLine="true"
                        android:inputType="number"
                        android:imeOptions="actionNext"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:text=":"/>

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/border_bottom"
                        android:layout_weight="1"
                        android:id="@+id/minutesMbajtje"
                        android:textSize="@dimen/font_size"
                        android:gravity="center"
                        android:text="20"
                        android:paddingBottom="@dimen/padding_spiner"
                        android:singleLine="true"
                        android:inputType="number"
                        android:imeOptions="actionNext"/>

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_below="@+id/detajetMbajtjesText"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="@dimen/vertical_padding"
            android:id="@+id/duttyCount"
            android:visibility="gone">

        <LinearLayout
            android:layout_width="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/duttyView"
            android:layout_alignParentLeft="true"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="2"
                android:gravity="center_vertical"
                android:layout_marginRight="@dimen/margin_spiner"
                android:text="Detyra e :"/>

            <Spinner
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/duttyCountS"
                android:background="@drawable/border_bottom"
                android:layout_weight="1"
                android:gravity="center"
                android:text="parë"/>

        </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:orientation="horizontal"
                android:layout_alignParentRight="true"
                android:id="@+id/checkboxPresentationLinear"
                android:layout_height="wrap_content">

                <CheckBox
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Prezantim"
                    android:layout_gravity="right"
                    android:id="@+id/presentationDutty"
                    android:imeOptions="actionNext"/>

            </LinearLayout>

        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/duttyCount"
            android:id="@+id/detajetMbajtjesPrezantimText"
            android:orientation="horizontal"
            android:visibility="gone"
            android:showDividers="end">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginRight="@dimen/general_margin"
                android:layout_weight="3">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:layout_weight="1"
                    android:text="Data e prezantimit:"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:id="@+id/datesPresentation"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="3dp"
                    android:orientation="horizontal">

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/dayPresentation"
                        android:background="@drawable/border_bottom"
                        android:layout_weight="1"
                        android:textSize="@dimen/font_size"
                        android:gravity="center"
                        android:layout_marginRight="5dp"
                        android:text="12"
                        android:paddingBottom="@dimen/padding_spiner"
                        android:singleLine="true"
                        android:inputType="number"
                        android:imeOptions="actionNext"/>

                    <Spinner
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/monthPresentation"
                        android:background="@drawable/border_bottom"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:textSize="@dimen/font_size"
                        android:layout_marginRight="5dp"
                        android:text="02"/>

                    <Spinner
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/yearPresentation"
                        android:background="@drawable/border_bottom"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:textSize="@dimen/font_size"
                        android:text="2016"/>

                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginLeft="@dimen/general_margin"
                android:layout_weight="2">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="15dp"
                    android:layout_weight="1"
                    android:text="Ora e prezantimit:"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:layout_marginTop="3dp"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/hourPresentation"
                        android:background="@drawable/border_bottom"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:textSize="@dimen/font_size"
                        android:text="22"
                        android:paddingBottom="@dimen/padding_spiner"
                        android:singleLine="true"
                        android:inputType="number"
                        android:imeOptions="actionNext"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="center"
                        android:text=":"/>

                    <EditText
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/border_bottom"
                        android:layout_weight="1"
                        android:id="@+id/minutesPresentation"
                        android:textSize="@dimen/font_size"
                        android:gravity="center"
                        android:text="20"
                        android:paddingBottom="@dimen/padding_spiner"
                        android:singleLine="true"
                        android:inputType="number"
                        android:imeOptions="actionNext"/>

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/psText"
            android:layout_marginTop="@dimen/general_margin"
            android:layout_below="@+id/detajetMbajtjesPrezantimText"
            android:text="P.s."/>

        <EditText
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:inputType="textMultiLine"
            android:id="@+id/psTextArea"
            android:padding="@dimen/padding_spiner"
            android:layout_below="@+id/psText"
            android:layout_marginTop="@dimen/padding_spiner"
            android:textSize="@dimen/font_size"
            android:gravity="top"
            android:background="@drawable/border_textarea"
            android:imeOptions="actionDone"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/previewMbajtje"
            android:layout_below="@+id/psTextArea"
            android:layout_centerHorizontal="true" />

    </RelativeLayout>

</ScrollView>

我希望有人能告诉我如何优化这个 xml 代码?

最佳答案

您的布局结构很糟糕,必然会导致性能问题。根据您的情况,有很多方法可以优化它:

  1. 周围的FrameLayout可以被删除,因为它只包含一个ScrollView。让 ScrollView 成为你的根。
  2. 请勿在布局顶部使用 RelativeLayout。每个 child 需要两张布局通行证。使用 LinearLayout 因为您只是按垂直顺序对齐 View
  3. 您至少有一个 LinearLayout 只包装了另一个 LinearLayout。您应该将它们合并为一个。
  4. 您有一个带有权重的 LinearLayout,它位于另一个带有权重的 LinearLayout 中。这也需要多次布局。
  5. 在另一个水平 LinearLayout 内有一个水平 LinearLayout。您或许也可以合并它们。

还有很多关于该做什么和不该做什么的教程。

关于java - Android XML 设计(布局)使我的应用程序如此缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35384833/

相关文章:

php - 将文件从实时服务器上传到 youtube 不起作用?

java - Android Studio : Code is done, 但每次我尝试运行时应用程序都会崩溃

Javaparser:用一种方法访问所有节点类型

java - 在 ssl (ldaps) 的支持下连接 Activity 目录

java - 使用 javascript 进行 Ajax 和 java servlet 数据传输

Java 俄罗斯方 block -如何更改一件掉落的计时器间隔,然后恢复回来?

php - 将 PHP 网络服务与 Android 连接

android - 在操作栏选项卡中添加 Page Curl

android - Unresolved reference :build.gradle.kts中的grgit

xml - 在 VBA 中获取 XML 响应中的值