2016-02-13 27 views
0

我爲我的應用程序設計了一個設計,但運行速度非常慢。我試圖達到的結果是這樣的: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代碼?

+0

這是什麼慢?滾動?提交數據?還有別的嗎?你爲什麼認爲它的xml-你運行了哪些測試來確定緩慢的部分是什麼? (雖然一般來說 - 較低的視圖深度(較少的嵌套視圖)對繪製perf更快,並且您的嵌套深度相當)。 –

+0

那麼當我點擊editTexts並寫入smth並隱藏鍵盤時,需要2或3秒才能隱藏鍵盤和鍵盤獲取全部白色的空間,持續2或3秒。 – ASTeam

+0

好的,所以既然你有一個滾動視圖,你將在輸入法的adjustResize模式。是的,你的xml可能是這樣的原因,但不是它的繪圖性能 - 這可能是佈局性能。 –

回答

3

你有一個糟糕的佈局結構,必然會出現性能問題。有許多方法來優化它,你的情況:

  1. 周圍FrameLayout可以去掉,因爲它只是持有ScrollView。使滾動視圖成爲根。
  2. 請勿在佈局頂部使用RelativeLayout。這將需要每個孩子2佈局通行證。使用LinearLayout,因爲您只是按垂直順序排列視圖
  3. 您至少有一個LinearLayout只包含另一個LinearLayout。你應該把它們合併成一個。
  4. 你有一個LinearLayout權重在另一個LinearLayout與權重。這也需要多個佈局傳遞。
  5. 在另一個水平LinearLayout內有一個水平的LinearLayout。你也可以合併這些。

還有很多關於做什麼和不做什麼的教程。

+0

謝謝,我遵循你所說的和它的工作順利 – ASTeam

+1

@ASTeam如果回答了你的問題,也請接受答案 –

相關問題