0

中佈局更改元素的位置我有這個佈局,當我在第一個模擬器上運行應用程序時,佈局顯示所有元素,如我所願。但是當我在具有較小屏幕的第二個模擬器上啓動它時,佈局的元素會被錯過並且未被排列。由於屏幕尺寸

我的問題:如何使所有設備獨立佈局,因爲並非所有設備都具有相同的屏幕尺寸。換句話說,即使模擬器的屏幕尺寸發生改變,我也希望元素的位置保持不變。

這是第一個模擬器截圖:

enter image description here

這是第二個模擬器截圖:

enter image description here

而這正是佈局代碼:

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

    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/light_primary_color" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginLeft="@dimen/feed_item_margin" 
     android:layout_marginRight="@dimen/feed_item_margin" 
     android:layout_marginTop="@dimen/feed_item_margin" 
     android:background="@drawable/bg_parent_rounded_corner" 
     android:orientation="vertical" 
     android:paddingBottom="@dimen/feed_item_padding_top_bottom" 
     android:paddingTop="@dimen/feed_item_padding_top_bottom" > 


     <com.android.volley.toolbox.NetworkImageView 
      android:id="@+id/summary_image_View" 
      android:paddingLeft="5dp" 
      android:paddingRight="5dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:layout_marginBottom="20dp" 
      android:scaleType="fitXY" 
      /> 

     <LinearLayout 
     android:weightSum="3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:paddingLeft="@dimen/feed_item_padding_left_right" 
      android:paddingRight="@dimen/feed_item_padding_left_right" > 


      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_weight="1" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 
       android:paddingLeft="@dimen/feed_item_profile_info_padd" > 
       <com.android.volley.toolbox.NetworkImageView 
        android:id="@+id/summary_first_team_logo" 
        android:layout_width="60dp" 
        android:layout_height="60dp" 
        android:scaleType="fitCenter" /> 
       <TextView 
        android:id="@+id/summary_first_team" 
        android:layout_marginTop="20sp" 
        android:layout_marginLeft="20sp" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:textColor="@color/primary_dark_color" 
        android:textSize="20sp" 
        android:text="Test" 

        /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_weight="1" 
       android:layout_width="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingLeft="@dimen/feed_item_profile_info_padd" > 

       <TextView 
        android:id="@+id/summary_match_date" 
        android:layout_width="match_parent" 
        android:background="@color/light_primary_color" 
        android:layout_height="wrap_content" 
        android:textAlignment="center" 
        android:textColor="@color/primary_text" 
        android:textSize="20sp" 
        android:text="Test" 

        /> 

       <TextView 
        android:textAlignment="center" 
        android:id="@+id/summary_match_result" 
        android:layout_width="match_parent" 
        android:background="@color/light_primary_color" 
        android:layout_height="wrap_content" 
        android:textColor="@color/primary_text" 
        android:textSize="20sp" 
        android:text="Test" 

        /> 
      </LinearLayout> 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_marginLeft="10sp" 
       android:layout_weight="1" 
       android:paddingLeft="20sp" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal" 
       > 

       <TextView 

        android:id="@+id/summary_second_team" 
        android:layout_marginTop="20sp" 
        android:layout_marginRight="10sp" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="@color/primary_dark_color" 
        android:textSize="20sp" 
        android:text="Test" 

        /> 

       <com.android.volley.toolbox.NetworkImageView 
        android:id="@+id/summary_second_team_logo" 
        android:layout_width="60dp" 
        android:layout_height="60dp" 
        android:scaleType="fitCenter" /> 
      </LinearLayout> 


     </LinearLayout> 


    </LinearLayout> 

</LinearLayout> 

回答

0

而不是以上述方式實現選項卡,我會看看TabLayout小部件。

它允許您更容易地製作標籤,通常更好的做法是使用TabLayout而不是大量的LinearLayout s)。

使用TabLayout還允許您設置模式(使用setTabMode(int)),因此您可以使您的選項卡可滾動,以便它們適合更小的屏幕尺寸。

+0

先生,謝謝你的回覆。但我希望線性佈局適合屏幕不僅是標籤。 另外,我想我不能使用TabLayout而不是LinearLayout,因爲我必須從MainActivity初始化它們。我只是希望佈局中元素的位置保持靜態,即使設備屏幕很小或很大。 –

+0

@DodoDodo讓窗口小部件保持靜態位置,以適應不同的屏幕尺寸,並且不鼓勵密度。你的佈局應該是敏感的,並且適應設備的屏幕(不像你在模擬器中顯示的那樣,而是以更清潔,更好,更好設計的方式)。如果屏幕上的所有視圖都保留在沒有文字的所有屏幕尺寸的相同位置上,例如繼續換行,將會很困難。 –

0

由於您在線性佈局中使用android:weightSum,我建議在子線性佈局中使用android:weightSum,因爲您正在爲圖像視圖指定「60dp」,併爲其分配一些權重。