Cardview隐藏了一些元素,Scrollbars无法正常工作

时间:2016-11-29 15:59:17

标签: android xml android-layout android-fragments android-cardview

我有这个布局文件,用于在内容超出屏幕高度的任何时候,不显示最后一个textview元素tv_notice_date的片段。我在cardview中添加了一个滚动条,但滚动条没有显示。

fragment_notice.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layout_margin="4dp"
        card_view:cardCornerRadius="4dp"
        card_view:cardElevation="1dp">

       <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <!--Title of notice-->
            <TextView
                android:id="@+id/tv_notice_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="Title of Notice"
                android:layout_gravity="center"
                android:textAppearance="?android:textAppearanceLarge"
                android:textSize="20sp"
                android:textStyle="bold"/>

          <TextView
               android:id="@+id/tv_notice_summary"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:padding="10dp"
               android:text="@string/notice_body"
               android:textAppearance="?android:textAppearanceMedium"
               android:textSize="15sp"/>
        <!--Date of Notice-->
        <TextView
            android:id="@+id/tv_notice_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:text="Mon, Nov 28, 2016. 8:56 AM"
            android:textColor="#0D47A1"/>
    </LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>

的strings.xml

<string name="notice_body">
    Summary of the very first post...Summary of the very first post...Summary of the very first post...
    Summary of the very first post...Summary of the very first post...Summary of the very first post...
    Summary of the very first post...Summary of the very first post...Summary of the very first post...
    Summary of the very first post...Summary of the very first post...Summary of the very first post...
    Summary of the very first post...Summary of the very first post...Summary of the very first post...
    Summary of the very first post...Summary of the very first post...Summary of the very first post...
    Summary of the very first post...Summary of the very first post...Summary of the very first post...
    Summary of the very first post...Summary of the very first post...Summary of the very first post...
    Summary of the very first post...Summary of the very first post...Summary of the very first post...
    Summary of the very first post...Summary of the very first post...Summary of the very first post...
</string>

我想在内容更多时显示滚动条,以便显示日期文本视图。

2 个答案:

答案 0 :(得分:1)

要在卡片外部滚动,您可以使用以下结构:

<ScrollView 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <!-- your content here -->
        </LinearLayout>
    </android.support.v7.widget.CardView>
</ScrollView>

您可以在卡片内部滚动,但在这种情况下,即使内容不多,卡片也会占据整个屏幕高度:

<android.support.v7.widget.CardView 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <!-- your content here -->
        </LinearLayout>
    </ScrollView>
</android.support.v7.widget.CardView>

答案 1 :(得分:0)

我不确定但是如果那不起作用那么你可以试试

LinearLayout
....
...>
<ScrollView
.....
...>
<CardView
.... 
....>
....
other views and Viewgroups
....
</CardView
</ScrollView>
</LinearLayout>

回复你得到的东西!!请告诉我们