x10*_*ion 6 xml android android-layout android-fragments
我在TabLayout中有一个片段xml.该TabLayout处于CollapsingToolbar布局滚动片段在TabLayout内容时下降,这崩溃.我有一个片段,我需要在recyclerView上面的TextView.
<LinearLayout>
<NestedScrollView
<TextView>
</TextView>
</NestedScrollView>
<View>
</View>
<RecyclerView>
</RecyclerView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
它工作正常,直到TextView有足够的内容填充或占用大部分屏幕,RecyclerView最终使用视图中的剩余空间显示:
Run Code Online (Sandbox Code Playgroud)|------------------| |<TextView-------->| |<---------------->| |<---------------->| |<---------------->| |<---------------->| |</TextView------->| |<RecyclerView---->| |</RecyclerView--->| |__________________|
因此,Recyclerview留有最小的空间可供查看.如果Textview占用整个屏幕,则RecyclerView不会显示.
取自这个SO问题 如果布局是:
<FrameLayout>
<NestedScrollView
<TextView>
</TextView>
</NestedScrollView>
<View>
</View>
<RecyclerView>
</RecyclerView>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
只显示recyclerView,而TextView只是不存在.
如果布局是:
<NestedScrollView>
<LinearLayout
<TextView>
</TextView>
<View>
</View>
<RecyclerView>
</RecyclerView>
</LinearLayout>
</NestedScrollView>
Run Code Online (Sandbox Code Playgroud)
TextView只显示RecyclerView中是否有内容.
如何让TextView滚动窗口足以显示recyclerview,以便屏幕可以从这个:
Run Code Online (Sandbox Code Playgroud)|------------------| |<TextView-------->| |<---------------->| |<---------------->| |<---------------->| |<---------------->| |</TextView------->| |<RecyclerView---->| |</RecyclerView--->| |__________________|
对此:
Run Code Online (Sandbox Code Playgroud)|------------------| |<---------------->| |</TextView------->| |<RecyclerView---->| |<---------------->| |<---------------->| |<---------------->| |<---------------->| |</RecyclerView--->| |__________________|
我当前的XML代码,其中只显示RecyclerView而不是TextView:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/item_shipping_shipping_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|left"
android:padding="@dimen/margin_16"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.v4.widget.NestedScrollView>
<View
android:id="@+id/line43"
android:layout_width="match_parent"
android:layout_height="@dimen/line_height"
android:background="@color/light_gray"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/item_shipping_fragment_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)
尝试为两种布局添加权重.
例如,如果您希望TextView部件小于或等于RecyclerView部件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_shipping_shipping_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start|left"
android:padding="@dimen/margin_16"/>
</android.support.v4.widget.NestedScrollView>
<View
android:id="@+id/line43"
android:layout_width="match_parent"
android:layout_height="@dimen/line_height"
android:background="@color/light_gray"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/item_shipping_fragment_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这样,包含TextView的布局将知道它应该一方面包装内容,但另一方面具有与RecyclerView相同的权重,并且它将在它们之间选择最小的内容.