【问题标题】:Set margin of a scrollview设置滚动视图的边距
【发布时间】:2012-09-18 13:41:23
【问题描述】:

我正在尝试在框架布局中使用滚动视图。 滚动视图应显示在显示屏的下半部分。

目前定位正确:

http://www.gtv-handball.de/Unbenannt.png

如您所见,正确的区域是否可缩放。 但问题是,上半部分不能再点击了,因为滚动视图在这部分上面。

为了在下半部分显示滚动视图,我在上面加了一些填充:

scrollviwe = (ScrollView)findViewById(R.id.linlayout);
scrollview.setPadding(0, 175, 0, 0);

所以我没有找到任何方法来使用填充选项显示它。

我认为,没有办法绕过为滚动视图设置边距, 但是我该怎么做呢?

这里是完整但缩短的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   android:orientation="vertical" >
    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top|left" >
         <ImageView
         android:id="@+id/coverimg"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="top|left"
         android:maxHeight="100dp"
         android:minHeight="130dp"
         android:minWidth="130dp"
         android:src="@drawable/cover_img" />
</FrameLayout>
<ScrollView
android:id="@+id/linlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/linlayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
    <Button
    android:id="@+id/teilen"
    style="ButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/btn_blue"
    android:gravity="center_vertical|center_horizontal"
    android:padding="4dp"
    android:text="Teilen"
    android:layout_margin="4dp"
    android:layout_weight="1" />
    <Button
    android:id="@+id/kaufen"
    style="ButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/btn_blue"
    android:gravity="center_vertical|center_horizontal"
    android:padding="4dp"
    android:text="Kaufen"
    android:layout_margin="4dp"
    android:layout_weight="1"/>
    <Button
    android:id="@+id/youtube"
    style="ButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/btn_blue"
    android:gravity="center_vertical|center_horizontal"
    android:padding="4dp"
    android:text="YouTube"
    android:layout_margin="4dp"
    android:layout_weight="1" />
</LinearLayout>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Zuletzt gehört:" />
</LinearLayout>
</ScrollView>
</FrameLayout>

【问题讨论】:

  • 如果你把滚动视图放在另一个框架布局的顶部,它肯定不会起作用。使用线性布局(垂直)并将每个高度为 0dp,重量为 1
  • 我应该把线性布局放在哪里?在第一个框架布局之后和滚动视图之前?
  • 而不是第一个框架布局
  • 那么我无法管理重叠的某些视图。
  • 没有意识到它们是重叠的。使用相对布局,然后

标签: java android layout scrollview android-framelayout


【解决方案1】:

您是否尝试过使用RelativeLayout?如果我正确理解您的目标,管理屏幕上所有不同的视图可能会更容易,因为您可以将视图相对于彼此或屏幕边框对齐。它也可以以与 FrameLayouts 类似的方式使用,将视图相互叠加。

【讨论】:

  • 我试过了,但我最终使用了框架布局。我在相对布局中定位不同视图时遇到了一些困难......
  • 我将布局更改为相对布局。一切正常。谢谢亚伦。
  • 太棒了!很高兴你能够让它工作。相对布局有时会很棘手,但它们确实有助于使代码更加简洁。
【解决方案2】:

尝试为 ScrollView 之外的 View 设置边距或填充。在这种情况下,它将是 FrameLayout。

【讨论】:

    【解决方案3】:

    我建议你包装一个线性布局。因为使用权重更容易管理它在屏幕上占用的空间。

    你可以试试这个。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1" >
    
    <LinearLayout
    android:id="@+id/framelayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top|left"
    android:layout_weight="0.5" 
    android:orientation="horizontal" 
    android:weightSum="1">
    
    <ImageView
        android:id="@+id/coverimg"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="top|left"
        android:maxHeight="100dp"
        android:minHeight="130dp"
        android:minWidth="130dp"
        android:src="@drawable/ic_launcher"
        android:layout_weight="0.5" />
    <LinearLayout android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5">
        <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Add Text"/>
        <!-- Add other views -->
      </LinearLayout>
    </LinearLayout>
    
    <ScrollView
    android:id="@+id/linlayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5" >
    
    <LinearLayout
        android:id="@+id/linlayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/teilen"
                style="ButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:gravity="center_vertical|center_horizontal"
                android:padding="4dp"
                android:text="Teilen" />
    
            <Button
                android:id="@+id/kaufen"
                style="ButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:gravity="center_vertical|center_horizontal"
                android:padding="4dp"
                android:text="Kaufen" />
    
            <Button
                android:id="@+id/youtube"
                style="ButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="4dp"
                android:layout_weight="1"
                android:gravity="center_vertical|center_horizontal"
                android:padding="4dp"
                android:text="YouTube" />
        </LinearLayout>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Zuletzt gehört:" />
    </LinearLayout>
    </ScrollView>
    </LinearLayout>
    

    【讨论】:

    • 问题是,我正在以编程方式设置一些边距,如下所示: FrameLayout fl = (FrameLayout)findViewById(R.id.framelayout); FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(width, width/2); fl.setLayoutParams(lp);我得到 09-18 14:16:29.665: E/AndroidRuntime(772): FATAL EXCEPTION: main 09-18 14:16:29.665: E/AndroidRuntime(772): java.lang.ClassCastException: android.widget.FrameLayout $LayoutParams
    • 你为哪些组件设置了参数。
    • 框架布局中的所有组件。
    • 在我建议的答案中,只有一个组件,即框架布局中的图像视图。我是否应该假设您仅为该组件应用新参数值。您能否通过以编程方式更改参数来阐明您计划实现的目标。
    • 我在不同类型和尺寸的显示器上实现了相同的布局。将所有这些视图放在相对布局中会更好吗,您可以在我发布的图片中看到它们?
    猜你喜欢
    • 2011-10-28
    • 1970-01-01
    • 2021-12-06
    • 2016-02-28
    • 2017-10-02
    • 2012-01-19
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    相关资源
    最近更新 更多