【问题标题】:Position of elements inside layout change because screen size布局内元素的位置因屏幕大小而变化
【发布时间】:2016-08-16 22:23:43
【问题描述】:

我有这个布局,当我在第一个模拟器上运行应用程序时,布局显示了我想要的所有元素。但是当我在具有较小屏幕的第二个模拟器上启动它时,布局的元素会被遗漏并且没有排列。

我的问题:如何使所有设备的布局独立,因为并非所有设备都具有相同的屏幕尺寸。换句话说,即使模拟器的屏幕尺寸发生变化,我也希望元素的位置保持不变。

这是第一个模拟器截图:

这是第二个模拟器截图:

这是布局代码:

<?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>

【问题讨论】:

    标签: android android-layout layout android-linearlayout android-relativelayout


    【解决方案1】:

    我不会像上面那样实现选项卡,而是看一下TabLayout 小部件。

    它使您可以更轻松地制作标签,通常使用TabLayout 而不是大量的LinearLayouts 是更好的做法。

    使用TabLayout 还允许您设置模式(使用setTabMode(int)),这样您就可以使标签可滚动,以便它们更适合较小的屏幕尺寸。

    【讨论】:

    • 先生,感谢您的回复。但我希望线性布局不仅适合选项卡,而且适合屏幕。另外,我认为我不能使用 TabLayout 代替 LinearLayout,因为我必须从 MainActivity 初始化它们。我只希望布局中元素的位置保持不变,即使设备屏幕很小或很大。
    • @DodoDodo 不鼓励小部件针对不同的屏幕尺寸和密度保持在静态位置。您的布局应该响应并适应设备的屏幕(不是像您在模拟器中显示的那样,而是以更清洁、更好、设计良好的方式)。在没有文字的情况下,很难让屏幕上的所有视图在所有屏幕尺寸上保持相同的位置,例如,在新的一行上继续。
    【解决方案2】:

    当您在线性布局中使用android:weightSum 时,我建议您在子线性布局中使用android:weightSum,因为您在内部为图像视图分配“60dp”,并为其分配一些权重。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 2019-09-04
      • 2016-10-07
      • 2018-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多