android - 动态添加项目到 ScrollView 使其不滚动

标签 android dynamic scrollview

我有一个简单的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="300dp"
android:padding="15dp">


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/scrollLayout">

        </LinearLayout>

    </ScrollView>
</RelativeLayout>

现在,我膨胀外部 RelativeLayout 以检索内部 LinearLayout 以将项目放入其中。

RelativeLayout relative =  (RelativeLayout) LayoutInflater.from(activity).inflate(R.layout.gradient_pick_view, null);
LinearLayout view = (LinearLayout) relative.findViewById(R.id.scrollLayout);

之后我创建了一个方法来向它添加一些按钮:

for(int i=0;i<10;i++){
     LinearLayout wrapper = (LinearLayout) LayoutInflater.from(activity).inflate(R.layout.button_wrapper, null);
     Button button = (Button)wrapper .findViewById(R.id.button);
     view.addView(layout);
}

一切正常,但它不滚动。 我在这里做错了什么?

这是屏幕截图(显示 10 个按钮中的 7 个): enter image description here

我忘了说 - 我正在使用 MaterialDialog 库并将此 RelativeLayout 作为自定义 View 添加到对话框中。

最佳答案

尝试将以下属性设置为您的 ScrollView ,

android:fillViewport="true"

以上属性用于使 ScrollView 使用应用程序的整个屏幕。

关于android - 动态添加项目到 ScrollView 使其不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42578136/

相关文章:

android - 如何从 React Navigation Header Ba 中调用类函数

javascript - 动态加载/卸载 Javascript

ios - 全屏 View 在 HStack 和水平 ScrollView 中不起作用

android - 如何在 Xamarin .axml for Android 中应用自定义 View

c# - 如何在 C# wpf Kinect SDK 2.0 中获取滚动查看器中的缩放值?

android - 在 Android 中查找要与 ComponentName 类一起使用的类

Android NDK - 无法加载库 : load_library on Android 17 and below

c# - 使用服务器端事件处理程序以编程方式创建链接按钮

c++ - 为回文遍历网格

java - 如何使用transition改变android studio中圆形按钮的颜色