I'm trying to achieve a simple screen where I have a horizontal scroll view with book entries. The problem I'm having is that it seems to cut off prematurely, cutting off entries. I have looked at other questions similar to this, and their fixes don't seem to fix mine, so I'm not sure how to handle this.
Here is what the cutting off looks like:
And here is my code:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#D3D3D3" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:orientation="vertical" >
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="@string/af"
android:textAppearance="?android:attr/textAppearanceLarge" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#A1A1A1" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal|end"
android:orientation="horizontal" >
<RelativeLayout
android:id="@+id/relative1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="168dp"
android:layout_height="258dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:contentDescription="@string/cd"
android:src="@drawable/af1" />
<TextView
android:id="@+id/textLabel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView1"
android:layout_centerInParent="true"
android:singleLine="true"
android:text="Guilty Wives" />
</RelativeLayout>
/*RelativeLayout repeats with different data, cut for brevity*/
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
this is a known bug that has never been solved: ( https://code.google.com/p/android/issues/detail?id=20088 ), but here is the code that worked for me. the trick is to set the horizontal scroll view's width to wrap content and the width of the linear (or relative) layout below it to match parent .
<HorizontalScrollView android:id="@+id/group_scroller" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioGroup android:id="@+id/choices_group" android:layout_width="wrap_content" android:orientation="horizontal" android:paddingTop="4dp" android:paddingBottom="4dp" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:gravity="center_horizontal" /> </LinearLayout> </HorizontalScrollView>
I managed to fix it by adding android:paddingRight="150dip"
to the LinearLayout.
That being said, I'm guessing this fix is hacky, and not actually solving the initial problem.
I tried all the solutions I could find on the net but none of them worked for a Relative Layout inside a HorizontalScrollView, so what I did is just a workaround. After adding all my views inside the layout, I add an invisible "line" after as being the last child on my layout, and position it to the right of my last actual view. This is a bit hacky but it's the least complicated and efficient way to do it without implementing unnecessary custom views.
View shim = new View(sender.getApplicationContext());
RelativeLayout.LayoutParams shimParams = new RelativeLayout.LayoutParams(0,ViewGroup.LayoutParams.MATCH_PARENT);
shimParams.addRule(RelativeLayout.RIGHT_OF,yourViewIDHere);
shim.setLayoutParams(shimParams);
YourLayout.addView(shim);
Works like a charm, and hope this helps if anyone stumbles upon it while searching for something that works.
Remove android:layout_gravity="center_horizontal" in linear layout.
<HorizontalScrollView
android:layout_gravity="center_horizontal"
android:scrollbars="none"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linear_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
......
......
</LinearLayout>
</HorizontalScrollView>
I was using
android:layout_gravity="center"
in my linear layout. Removed that and it worked fine
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.