【问题标题】:How to use scrollview for showing third cards after scrolling?滚动后如何使用滚动视图显示第三张卡片?
【发布时间】:2018-01-17 18:52:33
【问题描述】:

我有三张牌。我想一次显示两张卡片,当用户向下滚动时,用户可以看到第三张卡片。但现在它通过降低每张卡片的高度一次显示三张卡片。我想要三张相同高度的卡片。

这是我的布局 xml 文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.coderz.creative.music.Fragment.HomeFragment">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/chart_layout"
        android:orientation="vertical"
        >

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            >
            <com.github.mikephil.charting.charts.PieChart
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/pie_chart_spend"
                >
            </com.github.mikephil.charting.charts.PieChart>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            >
            <com.github.mikephil.charting.charts.BarChart
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/bar_chart_spend"
                >
            </com.github.mikephil.charting.charts.BarChart>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            >
            <com.github.mikephil.charting.charts.BarChart
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/pie_chart_spend_on_click"
                >
            </com.github.mikephil.charting.charts.BarChart>
        </android.support.v7.widget.CardView>
    </LinearLayout>


</FrameLayout>

【问题讨论】:

  • 您想显示两张卡片(填满整个屏幕)和它们下方的第三张卡片,它们都具有相同的高度?
  • 是的 elmorabea。我想要这个。

标签: android scrollview android-linearlayout


【解决方案1】:

尝试使用 ScrollView,如下所示。我已将卡片的高度设置为 250dp,但您可以根据需要进行调整。请记住,由于高度是固定的,因此它在不同的屏幕尺寸上看起来会有所不同。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.coderz.creative.music.Fragment.HomeFragment">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/chart_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_margin="10dp">

                <com.github.mikephil.charting.charts.PieChart
                    android:id="@+id/pie_chart_spend"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                </com.github.mikephil.charting.charts.PieChart>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_margin="10dp">

                <com.github.mikephil.charting.charts.BarChart
                    android:id="@+id/bar_chart_spend"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                </com.github.mikephil.charting.charts.BarChart>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_margin="10dp">

                <com.github.mikephil.charting.charts.BarChart
                    android:id="@+id/pie_chart_spend_on_click"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                </com.github.mikephil.charting.charts.BarChart>
            </android.support.v7.widget.CardView>
        </LinearLayout>
    </ScrollView>

</FrameLayout>

【讨论】:

  • 好的科伦斯。感谢您的回答。它正在按我的意愿工作。但是固定高度是更好的方法吗?除了固定高度还有其他更好的方法吗?
  • 是的,将父级的高度除以2,然后动态分配高度给你的卡片
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多