LinearLayout里面的Multiple RelativeLayout显示错误
Multiple RelativeLayout inside LinearLayout display error
我有一个 MoodTracker 应用程序,它每 24 小时将每日心情存储到首选项中,并显示最近 7 天心情的历史记录。
我的问题如下:
我的历史布局在 LinearLayout 中有 7 个相关布局(每天一个)。
预览是完美的,一个颠倒。但是当我运行申请的时候,
我只能看到 4/7 布局,每一个都比预期的要大......
这是我的代码:
XML : https://github.com/matteovaccari/MoodTracker/blob/master/app/src/main/res/layout/activity_mood_history.xml
java: https://github.com/matteovaccari/MoodTracker/blob/master/app/src/main/java/com/matt/android/moodtracker_v2/controllers/MoodHistoryActivity.java
提前致谢
问题出在你的 RelativeLayouts:
android:layout_height="match_parent"
android:layout_weight="1"
如果您使用 layout_weight 并且您希望 7 个 RelativeLayouts 占据相等的空间,则应设置 layout_height="0" space:
android:layout_height="0"
android:layout_weight="1"
在每个 Relative Layout 中进行以下更改:
- 将每个中的 'android: layout_height' 更改为 0
- 添加 'layout_weight' 属性并将其值设置为 1
不要在您的视图上使用 android:layout_weight
或固定大小 - 它可能适用于像这样的小布局,但在大布局中,它可能会影响您的屏幕性能,在视图上使用固定大小时对所有屏幕尺寸没有响应。
你最好使用 constraintlayout 来获得一个对所有设备都有响应的漂亮屏幕:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Fragments.MenusDesign.BookMenu.BookMenu">
<TextView
android:id="@+id/activity_historic_text_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/activity_historic_btn_five"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_text_six"
app:layout_constraintStart_toStartOf="@+id/activity_historic_text_six"
app:layout_constraintTop_toTopOf="@+id/activity_historic_btn_five"
tools:text="string/day_7" />
<Button
android:id="@+id/activity_historic_btn_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="6"
app:layout_constraintBottom_toTopOf="@+id/guideline11"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_btn_four"
app:layout_constraintStart_toStartOf="@+id/activity_historic_btn_four"
app:layout_constraintTop_toTopOf="@+id/guideline10" />
<TextView
android:id="@+id/activity_historic_text_six"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentStart="true"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/activity_historic_btn_seven"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_text_five"
app:layout_constraintStart_toStartOf="@+id/activity_historic_text_five"
app:layout_constraintTop_toTopOf="@+id/activity_historic_btn_seven"
tools:text="string/day_6" />
<Button
android:id="@+id/activity_historic_btn_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="4"
app:layout_constraintBottom_toTopOf="@+id/guideline9"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_btn_three"
app:layout_constraintStart_toStartOf="@+id/activity_historic_btn_three"
app:layout_constraintTop_toTopOf="@+id/guideline8" />
<TextView
android:id="@+id/activity_historic_text_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/activity_historic_btn_four"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_text_four"
app:layout_constraintStart_toStartOf="@+id/activity_historic_text_four"
app:layout_constraintTop_toTopOf="@+id/activity_historic_btn_four"
tools:text="string/day_5" />
<Button
android:id="@+id/activity_historic_btn_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_btn_seven"
app:layout_constraintStart_toStartOf="@+id/activity_historic_btn_seven"
app:layout_constraintTop_toTopOf="@+id/guideline11" />
<TextView
android:id="@+id/activity_historic_text_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/activity_historic_btn_six"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_text_three"
app:layout_constraintStart_toStartOf="@+id/activity_historic_text_three"
app:layout_constraintTop_toTopOf="@+id/activity_historic_btn_six"
tools:text="string/day_4" />
<Button
android:id="@+id/activity_historic_btn_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="5"
app:layout_constraintBottom_toTopOf="@+id/guideline10"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_btn_six"
app:layout_constraintStart_toStartOf="@+id/activity_historic_btn_six"
app:layout_constraintTop_toTopOf="@+id/guideline9" />" />
<TextView
android:id="@+id/activity_historic_text_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/activity_historic_btn_three"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_text_two"
app:layout_constraintStart_toStartOf="@+id/activity_historic_text_two"
app:layout_constraintTop_toTopOf="@+id/activity_historic_btn_three"
tools:text="string/day_3" />
<Button
android:id="@+id/activity_historic_btn_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="3"
app:layout_constraintBottom_toTopOf="@+id/guideline8"
app:layout_constraintEnd_toEndOf="@+id/button6"
app:layout_constraintStart_toStartOf="@+id/button6"
app:layout_constraintTop_toTopOf="@+id/guideline7" />
<TextView
android:id="@+id/activity_historic_text_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/button6"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_text_one"
app:layout_constraintStart_toStartOf="@+id/activity_historic_text_one"
app:layout_constraintTop_toTopOf="@+id/button6"
tools:text="string/day_2"/>
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="2"
app:layout_constraintBottom_toTopOf="@+id/guideline7"
app:layout_constraintEnd_toEndOf="@+id/button"
app:layout_constraintStart_toStartOf="@+id/button"
app:layout_constraintTop_toTopOf="@+id/guideline6" />
<TextView
android:id="@+id/activity_historic_text_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/button"
app:layout_constraintEnd_toStartOf="@+id/guideline12"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/button"
tools:text="string/day_1" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="1"
app:layout_constraintBottom_toTopOf="@+id/guideline6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline12"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.142" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.28" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.42" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.57" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.714" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.857" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
我有一个 MoodTracker 应用程序,它每 24 小时将每日心情存储到首选项中,并显示最近 7 天心情的历史记录。
我的问题如下:
我的历史布局在 LinearLayout 中有 7 个相关布局(每天一个)。
预览是完美的,一个颠倒。但是当我运行申请的时候,
我只能看到 4/7 布局,每一个都比预期的要大......
这是我的代码:
XML : https://github.com/matteovaccari/MoodTracker/blob/master/app/src/main/res/layout/activity_mood_history.xml
java: https://github.com/matteovaccari/MoodTracker/blob/master/app/src/main/java/com/matt/android/moodtracker_v2/controllers/MoodHistoryActivity.java
提前致谢
问题出在你的 RelativeLayouts:
android:layout_height="match_parent"
android:layout_weight="1"
如果您使用 layout_weight 并且您希望 7 个 RelativeLayouts 占据相等的空间,则应设置 layout_height="0" space:
android:layout_height="0"
android:layout_weight="1"
在每个 Relative Layout 中进行以下更改:
- 将每个中的 'android: layout_height' 更改为 0
- 添加 'layout_weight' 属性并将其值设置为 1
不要在您的视图上使用 android:layout_weight
或固定大小 - 它可能适用于像这样的小布局,但在大布局中,它可能会影响您的屏幕性能,在视图上使用固定大小时对所有屏幕尺寸没有响应。
你最好使用 constraintlayout 来获得一个对所有设备都有响应的漂亮屏幕:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Fragments.MenusDesign.BookMenu.BookMenu">
<TextView
android:id="@+id/activity_historic_text_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/activity_historic_btn_five"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_text_six"
app:layout_constraintStart_toStartOf="@+id/activity_historic_text_six"
app:layout_constraintTop_toTopOf="@+id/activity_historic_btn_five"
tools:text="string/day_7" />
<Button
android:id="@+id/activity_historic_btn_seven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="6"
app:layout_constraintBottom_toTopOf="@+id/guideline11"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_btn_four"
app:layout_constraintStart_toStartOf="@+id/activity_historic_btn_four"
app:layout_constraintTop_toTopOf="@+id/guideline10" />
<TextView
android:id="@+id/activity_historic_text_six"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentStart="true"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/activity_historic_btn_seven"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_text_five"
app:layout_constraintStart_toStartOf="@+id/activity_historic_text_five"
app:layout_constraintTop_toTopOf="@+id/activity_historic_btn_seven"
tools:text="string/day_6" />
<Button
android:id="@+id/activity_historic_btn_six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="4"
app:layout_constraintBottom_toTopOf="@+id/guideline9"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_btn_three"
app:layout_constraintStart_toStartOf="@+id/activity_historic_btn_three"
app:layout_constraintTop_toTopOf="@+id/guideline8" />
<TextView
android:id="@+id/activity_historic_text_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/activity_historic_btn_four"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_text_four"
app:layout_constraintStart_toStartOf="@+id/activity_historic_text_four"
app:layout_constraintTop_toTopOf="@+id/activity_historic_btn_four"
tools:text="string/day_5" />
<Button
android:id="@+id/activity_historic_btn_five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="7"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_btn_seven"
app:layout_constraintStart_toStartOf="@+id/activity_historic_btn_seven"
app:layout_constraintTop_toTopOf="@+id/guideline11" />
<TextView
android:id="@+id/activity_historic_text_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/activity_historic_btn_six"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_text_three"
app:layout_constraintStart_toStartOf="@+id/activity_historic_text_three"
app:layout_constraintTop_toTopOf="@+id/activity_historic_btn_six"
tools:text="string/day_4" />
<Button
android:id="@+id/activity_historic_btn_four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="5"
app:layout_constraintBottom_toTopOf="@+id/guideline10"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_btn_six"
app:layout_constraintStart_toStartOf="@+id/activity_historic_btn_six"
app:layout_constraintTop_toTopOf="@+id/guideline9" />" />
<TextView
android:id="@+id/activity_historic_text_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/activity_historic_btn_three"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_text_two"
app:layout_constraintStart_toStartOf="@+id/activity_historic_text_two"
app:layout_constraintTop_toTopOf="@+id/activity_historic_btn_three"
tools:text="string/day_3" />
<Button
android:id="@+id/activity_historic_btn_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="3"
app:layout_constraintBottom_toTopOf="@+id/guideline8"
app:layout_constraintEnd_toEndOf="@+id/button6"
app:layout_constraintStart_toStartOf="@+id/button6"
app:layout_constraintTop_toTopOf="@+id/guideline7" />
<TextView
android:id="@+id/activity_historic_text_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/button6"
app:layout_constraintEnd_toEndOf="@+id/activity_historic_text_one"
app:layout_constraintStart_toStartOf="@+id/activity_historic_text_one"
app:layout_constraintTop_toTopOf="@+id/button6"
tools:text="string/day_2"/>
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="2"
app:layout_constraintBottom_toTopOf="@+id/guideline7"
app:layout_constraintEnd_toEndOf="@+id/button"
app:layout_constraintStart_toStartOf="@+id/button"
app:layout_constraintTop_toTopOf="@+id/guideline6" />
<TextView
android:id="@+id/activity_historic_text_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/button"
app:layout_constraintEnd_toStartOf="@+id/guideline12"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/button"
tools:text="string/day_1" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="1"
app:layout_constraintBottom_toTopOf="@+id/guideline6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline12"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.142" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.28" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.42" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.57" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.714" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.857" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />