简体   繁体   English

线性布局或水平滚动视图正在切断内容

[英]Linear Layout or Horizontal Scroll View is cutting off content

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.这是一个从未解决的已知错误:( https://code.google.com/p/android/issues/detail?id=20088 ),但这是对我有用的代码。 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 .诀窍是设置水平滚动视图的宽度以包装内容,并设置其下方的线性(或相对)布局的宽度以匹配 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.我设法通过将android:paddingRight="150dip"到 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.我尝试了我在网上能找到的所有解决方案,但没有一个适用于 Horizo​​ntalScrollView 中的相对布局,所以我所做的只是一种解决方法。 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.这有点 hacky,但它是在不实现不必要的自定义视图的情况下最不复杂和最有效的方法。

    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.删除线性布局中的 android:layout_gravity="center_horizo​​ntal"。

   <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删除了它,它工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM