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”,并为其分配一些权重。