I am doing some stuff in Android 3.0 with fragments. In one of the fragments, I need the scroll bar. I tried using a ScrollView
, but it does not give me a scroll bar for the fragment.
How do I get a scroll bar fo开发者_如何学Cr a fragment?
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
//enclose your code for contents of fragment here. This helped me to add scroll view to the fragment
</ScrollView>
I've found an example in the official Android resources: FragmentLayout. The class DetailsFragment contains a ScrollView which contains a TextView.
I tried the example and it's working on 3.0 SDK Emulator. If it doesn't fix your problem, can you give more details of your code ?
Alternatively, you might prefer to just implement the ScrollView in an XML layout file: How to use ScrollView in Android?
Use ScrollView tags for only fragments, which you want to scroll. It is working in my project. This is just a suggestion because I don't know your project or what you want to scroll on the screen.
ScrollView cannot be used in a Fragment, since you cannot inflate many fragments under the same parent. You could rather use RelativeLayout or LinearLayout and keep ScrollView as inner layout.
Also checkout the new FragmentLayout in com.android.widget
folder.
In Android studio right click your project and select New
--> Fragment
--> Scrolling fragment
, so get a new scrolling fragment
If you already have an existing fragment, just check that android studio generated code and change your fragment code like the generated one.
Hope this helped.
Happy coding!
精彩评论