LinearLayout内的多个RelativeLayout显示错误

时间:2019-03-09 10:11:25

标签: android android-linearlayout android-relativelayout

我有一个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
预先感谢

3 个答案:

答案 0 :(得分:0)

问题出在您的RelativeLayouts中:

let categories = [{id: 1,name: "category 1",items: [19993, 99737]},{id: 2,name: "category 2",items: [70264, 65474, 07078]},{id: 3,name: "category 3",items: [76765]}]
let items = [{id: 19993, name: "item 1"},{id: 70264, name: "item 2"},{id: 99737, name: "item 3"},   {id: 65474, name: "item 4"},{id: 76765, name: "item 5"},{id: 07078, name: "item 6"},]

let op = categories.reduce((op, {name, items:cartItems}) => {
  cartItems.forEach(key => {
    let value = items.find(({id}) => id === key)
    if(value){
      op[name] = op[name] || []
      op[name].push(value)
    }
  })
  return op
},{})

console.log(op)

如果您使用的是layout_weight,并且您希望7个RelativeLayouts占用相等的空间,则应设置layout_height =“ 0”:

android:layout_height="match_parent"
android:layout_weight="1"

答案 1 :(得分:0)

在每个RelativeLayout中,进行以下更改:

  1. 将每个“ android:layout_height”更改为0
  2. 添加“ layout_weight”属性并将其值设置为1

答案 2 :(得分:0)

请勿在视图上使用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" />