适合屏幕尺寸且没有重叠的线性布局

问题描述 投票:1回答:1

我有一个活动的Android布局,如下所示。布局分为页眉和页脚。中间部分分为权重相等的段,每个段上方都有一个文本。使用这种布局,我有两个问题:

首先,当布局显示在小屏幕上时,文本和图像垂直重叠。我要的是图像和文本缩放,以使它们在任何屏幕尺寸上都不重叠。我认为可以通过将文本移到权重相等的部分(即@+id/row1@+id/row2@+id/row3)来实现。但是我不知道该怎么办,如果真的是解决方案。

有人知道该怎么办吗?

第二,标题中的问号相对于文本而言太大。在屏幕截图上,文本仅在一行上,因此问号应该较小。在较小的屏幕上,文本可能在两行上,然后问号应该变大。这意味着我该如何实现,以使问号适应文本的高度?

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/state_description"
            android:background="@color/eth"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginStart="30dp"
                    android:layout_marginEnd="100dp"
                    android:orientation="vertical"
                    android:gravity="start|center_vertical"
                    android:textSize="16sp"
                    android:text="This is a tile" />

                <ImageView
                    android:id="@+id/sam_help_button"
                    android:layout_width="45dp"
                    android:layout_height="45dp"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="10dp"
                    android:layout_marginBottom="10dp"
                    android:layout_marginEnd="10dp"
                    android:padding="5dp"
                    android:adjustViewBounds="true"
                    android:layout_centerVertical="true"
                    android:scaleType="fitCenter"
                    android:src="@drawable/question_mark_white"
                    android:background="@drawable/round_button"/>
            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:weightSum="3"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:textSize="16sp"
                android:text="This is the title of the first three images" />

            <!-- Row number 1 -->
            <LinearLayout
                android:id="@+id/row1"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginTop="@dimen/padding_contour"
                android:layout_marginBottom="0dp"
                android:layout_marginLeft="@dimen/padding_contour"
                android:layout_marginRight="@dimen/padding_contour"
                android:layout_weight="1">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:id="@+id/valence_pos_layout"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/valence_pos_image"
                            android:layout_width="65dp"
                            android:layout_height="50dp"
                            android:layout_gravity="center"
                            android:src="@drawable/ic_launcher_background" />

                        <TextView
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"
                            android:textAlignment="center"
                            android:layout_marginTop="10dp"
                            android:text="Positive"
                            android:textColor="@android:color/white"
                            android:textSize="14sp" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/valence_neutral_layout"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/valence_neutral_image"
                            android:layout_width="65dp"
                            android:layout_height="50dp"
                            android:layout_gravity="center"
                            android:src="@drawable/ic_launcher_background" />

                        <TextView
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"
                            android:textAlignment="center"
                            android:layout_marginTop="10dp"
                            android:text="Neutral"
                            android:textColor="@android:color/white"
                            android:textSize="14sp" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/valence_neg_layout"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:layout_weight="1"
                        android:orientation="vertical">

                        <ImageView
                            android:id="@+id/valence_neg_image"
                            android:layout_width="65dp"
                            android:layout_height="50dp"
                            android:layout_gravity="center"
                            android:src="@drawable/ic_launcher_background" />

                        <TextView
                            android:layout_width="100dp"
                            android:layout_height="wrap_content"
                            android:textAlignment="center"
                            android:layout_marginTop="10dp"
                            android:text="Negative"
                            android:textColor="@android:color/white"
                            android:textSize="14sp" />
                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:gravity="center"
                android:textSize="16sp"
                android:text="This is the title of the second three images" />

            <!-- Row number 2 -->
            <LinearLayout
                android:id="@+id/row2"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginTop="@dimen/padding_contour"
                android:layout_marginBottom="0dp"
                android:layout_marginLeft="@dimen/padding_contour"
                android:layout_marginRight="@dimen/padding_contour"
                android:layout_weight="1">

                <LinearLayout
                    android:id="@+id/arousal_high_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/arousal_high_image"
                        android:layout_width="65dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:src="@drawable/ic_launcher_background" />

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:textAlignment="center"
                        android:layout_marginTop="10dp"
                        android:text="High"
                        android:textColor="@android:color/white"
                        android:textSize="14sp" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/arousal_neutral_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/arousal_neutral_image"
                        android:layout_width="65dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:src="@drawable/ic_launcher_background" />

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:textAlignment="center"
                        android:layout_marginTop="10dp"
                        android:text="Medium"
                        android:textColor="@android:color/white"
                        android:textSize="14sp" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/arousal_low_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/arousal_low_image"
                        android:layout_width="65dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:src="@drawable/ic_launcher_background" />

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:textAlignment="center"
                        android:layout_marginTop="10dp"
                        android:text="Low"
                        android:textColor="@android:color/white"
                        android:textSize="14sp" />
                </LinearLayout>

            </LinearLayout>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:gravity="center"
                android:textSize="16sp"
                android:text="This is the title of the last three images" />

            <!-- Row number 3 -->
            <LinearLayout
                android:id="@+id/row3"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginTop="@dimen/padding_contour"
                android:layout_marginBottom="0dp"
                android:layout_marginLeft="@dimen/padding_contour"
                android:layout_marginRight="@dimen/padding_contour"
                android:layout_weight="1">

                <LinearLayout
                    android:id="@+id/dominance_low_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/dominance_low_image"
                        android:layout_width="65dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:src="@drawable/ic_launcher_background" />

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:textAlignment="center"
                        android:layout_marginTop="10dp"
                        android:text="Controlled"
                        android:textColor="@android:color/white"
                        android:textSize="14sp" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/dominance_neutral_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/dominance_neutral_image"
                        android:layout_width="65dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:src="@drawable/ic_launcher_background" />

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:textAlignment="center"
                        android:layout_marginTop="10dp"
                        android:text="Neutral"
                        android:textColor="@android:color/white"
                        android:textSize="14sp" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/dominance_high_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/dominance_high_image"
                        android:layout_width="65dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:src="@drawable/ic_launcher_background" />

                    <TextView
                        android:layout_width="100dp"
                        android:layout_height="wrap_content"
                        android:textAlignment="center"
                        android:layout_marginTop="10dp"
                        android:text="In-control"
                        android:textColor="@android:color/white"
                        android:textSize="14sp" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="vertical">

                <RelativeLayout
                    android:id="@+id/sam_button_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <Button
                        android:id="@+id/submit_sam_button"
                        android:layout_width="120dp"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="30dp"
                        android:layout_alignParentEnd="true"
                        android:textSize="14sp"
                        android:padding="10dp"
                        android:textColor="@android:color/white"
                        android:background="@drawable/button"
                        android:drawableStart="@drawable/ic_check_circle_white_24dp"
                        android:drawablePadding="10dp"
                        android:text="Next" />

                    <Button
                        android:id="@+id/cancel_sam_button"
                        android:layout_width="120dp"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="30dp"
                        android:layout_alignParentStart="true"
                        android:textSize="14sp"
                        android:padding="10dp"
                        android:textColor="@android:color/white"
                        android:background="@drawable/button"
                        android:drawableStart="@drawable/ic_cancel_white_24dp"
                        android:drawablePadding="10dp"
                        android:text="Cancel" />
                </RelativeLayout >

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

    <!-- Container for contents of drawer - use NavigationView to make configuration easier -->
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_view"
        app:headerLayout="@layout/nav_header" />
</androidx.drawerlayout.widget.DrawerLayout>

enter image description here

android android-layout android-linearlayout android-layout-weight
1个回答
0
投票
layout_height=60 layout_width=60

相反,您必须使用

layout_height="match_parent/wrap_content"
layout_width="match_parent/wrap_content"

看看这个,

https://developer.android.com/training/multiscreen/screensizes

https://www.youtube.com/watch?v=b6AVdCKoyiQ

您还应该使用layout_weight属性,

https://www.youtube.com/watch?v=swZhVj6r6x0

© www.soinside.com 2019 - 2024. All rights reserved.