【问题标题】:Linear Layout or Horizontal Scroll View is cutting off content线性布局或水平滚动视图正在切断内容
【发布时间】:2014-03-01 21:54:22
【问题描述】:

我正在尝试实现一个简单的屏幕,其中我有一个带有书籍条目的水平滚动视图。我遇到的问题是它似乎过早地切断,切断条目。我查看了与此类似的其他问题,他们的修复似乎无法解决我的问题,所以我不确定如何处理。

以下是截断的样子:

这是我的代码:

<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>

【问题讨论】:

    标签: android xml android-layout horizontalscrollview


    【解决方案1】:

    这是一个从未解决的已知错误:(https://code.google.com/p/android/issues/detail?id=20088), 但这是对我有用的代码。 诀窍是设置水平滚动视图的宽度以包裹内容,并在其下方设置线性(或相对)布局的宽度以匹配父级。

    <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>
    

    【讨论】:

      【解决方案2】:

      我设法通过将 android:paddingRight="150dip" 添加到 LinearLayout 来修复它。

      话虽这么说,我猜这个修复程序很老套,并没有真正解决最初的问题。

      【讨论】:

      【解决方案3】:

      我尝试了所有可以在网上找到的解决方案,但没有一个适用于 Horizo​​ntalScrollView 中的相对布局,所以我所做的只是一种解决方法。在布局中添加所有视图后,我在布局中的最后一个子元素之后添加了一条不可见的“线”,并将其放置在我最后一个实际视图的右侧。这有点 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);
      

      就像一个魅力,如果有人在寻找有用的东西时偶然发现它,希望这会有所帮助。

      【讨论】:

        【解决方案4】:

        删除 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>
        

        【讨论】:

          【解决方案5】:

          我正在使用

          android:layout_gravity="center"
          

          在我的线性布局中。 删除它,它工作正常

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-27
            • 2011-08-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多