【问题标题】:Many layouts in Scroll View in androidandroid中滚动视图中的许多布局
【发布时间】:2017-07-07 08:26:27
【问题描述】:

我想在我的 ScrollView 中有 3 个布局,但是当我添加它们时它实际上并没有滚动。我试图将所有布局放在不同的文件中,然后包含它们,试图将它们放在 ListView 中,但它也不起作用。在此选项中,当我将 ScrollView 放入 LinearLayout 并包含其余布局时,应用程序什么也不显示。这可能是因为我不知道如何引用嵌套布局...这是我的 XML:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
>
    <ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
        <LinearLayout android:layout_width="match_parent" 
                      android:layout_height="wrap_content"
                      android:orientation="vertical"
        >
            <include layout="@layout/first"/>
            <include layout="@layout/second"/>
            <include layout="@layout/third"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

@EDIT 视图包括(所有都相同,所以我只放一个):

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
>
</RelativeLayout>

主要活动中,我将对象放入如下布局中:

setContentView(R.layout.activity_main);
RelativeLayout first = (RelativeLayout) findViewById(R.id.first);
RelativeLayout second = (RelativeLayout) findViewById(R.id.second);
RelativeLayout third = (RelativeLayout) findViewById(R.id.third);

...some code creating views...

first.addView(myView1);
second.addView(myView2);
third.addView(myView3);

【问题讨论】:

  • 你在Relativelayouts中设置了android:layout_height="wrap_content" 吗?
  • 能否请您发布您包含的 3 个相对布局?
  • 是的,要更新问题
  • 从您迄今为止发布的代码中我可以看出:问题似乎出在您在“...一些代码创建视图...”部分中创建的视图中。跨度>
  • @Bmuig 你是对的,我将错误的布局参数传递给视图,非常感谢,问题解决了!

标签: android android-layout scrollview android-scrollview


【解决方案1】:

需要在包含的布局中放入fix size,例如:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/first"
    android:layout_width="match_parent"
    android:layout_height="300dp"

/>

我不明白为什么在已经将视图添加到布局时还需要手动添加视图。

您还应该删除顶部Linear Layout,因为它没有用处。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多