布局内元素的位置因屏幕大小而改变

时间:2016-04-23 07:56:15

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

我有这个布局,当我在第一个模拟器上运行应用程序时,布局显示我想要的所有元素。但是当我在具有较小屏幕的第二个仿真器上启动它时,布局的元素将被错过而不是被安排。

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

这是第一个模拟器截图:

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>

2 个答案:

答案 0 :(得分:0)

不是按照上面的方式实现标签,而是查看TabLayout小部件。

它允许您更轻松地制作标签,通常更好的做法是使用TabLayout而非许多LinearLayout s。

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

答案 1 :(得分:0)

当您在线性布局中使用android:weightSum时,我建议您在子线性布局中使用android:weightSum,因为您正在分配&#34; 60dp&#34;在图像视图中,为其指定一些权重。

相关问题