我为我的应用程序做了一个设计,但它运行速度非常慢。我想要实现的结果是这样的:
到目前为止,我已经能够使用此 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 代码?
最佳答案
您的布局结构很糟糕,必然会导致性能问题。根据您的情况,有很多方法可以优化它:
- 周围的
FrameLayout
可以被删除,因为它只包含一个ScrollView
。让 ScrollView 成为你的根。 - 请勿在布局顶部使用
RelativeLayout
。每个 child 需要两张布局通行证。使用LinearLayout
因为您只是按垂直顺序对齐 View - 您至少有一个
LinearLayout
只包装了另一个LinearLayout
。您应该将它们合并为一个。 - 您有一个带有权重的
LinearLayout
,它位于另一个带有权重的LinearLayout
中。这也需要多次布局。 - 在另一个水平
LinearLayout
内有一个水平LinearLayout
。您或许也可以合并它们。
还有很多关于该做什么和不该做什么的教程。
关于java - Android XML 设计(布局)使我的应用程序如此缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35384833/