Android XML 设计(布局)使我的应用程序变慢

AST*_*eam 0 java xml android

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

在此处输入图片说明

到目前为止,我已经能够用这个 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>
Run Code Online (Sandbox Code Playgroud)

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

Dav*_*jak 5

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

  1. 周围FrameLayout可以被移除,因为它只包含一个ScrollView. 使滚动视图成为您的根。
  2. 不要RelativeLayout在布局的顶部使用这么远的地方。每个孩子都需要 2 个布局通行证。使用 aLinearLayout因为您只是按垂直顺序对齐视图
  3. 你至少有一个LinearLayout只是包装另一个LinearLayout。您应该将它们合二为一。
  4. 你有一个LinearLayout带权重的另一个LinearLayout带权重。这也需要多次布局传递。
  5. LinearLayout在另一个水平中有一个水平LinearLayout。你也可以合并这些。

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

  • @ASTeam 如果回答了您的问题,也请接受答案 (2认同)