Android:ScrollView 无法与 View 类一起使用

标签 android android-layout view scrollview

我创建了一个用于绘制图形的类,该类扩展了 View,它看起来就像这里 How to draw a line in android .

在一个 Activity 中,我用这个来展示它

    drawView = new DrawView(this);
    drawView.setBackgroundColor(Color.WHITE);
    setContentView(drawView);

但是当我添加 ScrollView在 Activity 的 xml 中的 LinearLayout 中,它不起作用,我的意思是我无法向下滚动,就像没有 ScrollView . 可能是什么问题?

这是xml代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background_image"
android:orientation="vertical" >

</LinearLayout>
</ScrollView>

最佳答案

这是因为您使用不同的方法添加了两个 View :一个是系统从 xml 布局扩展它的,另一个是在 Activity 中创建的。
这样,当您调用时,xml 布局中的内容将被丢弃 设置内容 View (绘制 View );
更好地说,是 替换 为在 OnCreate 方法中创建的 DrawView
您应该在 Activity 中扩充 xml 布局:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ScrollView container = getLayoutInflater().inflate(R.layout.main_layout, null));
setContentView(drawView);
drawView = container.findViewById(R.id.my_draw_view);
drawView.setBackgroundColor(Color.WHITE);  
}

Activity 布局应如下所示:

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

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

<com.yourcompany.DrawView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>
</ScrollView>


如果有帮助,请不要害羞地接受它或 +1。

关于Android:ScrollView 无法与 View 类一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14056120/

相关文章:

Android:CursorLoader 在非最顶层的 Fragment 上崩溃

android - androidcamera2中的曝光补偿方法

android - 使用按位或向 LayoutParams.addRule() 发送多个标志是否有意义?

ios - 将各个 View 放入选项卡的最佳方式

mysql - 将函数应用于 View 中的多列

c# - Xamarin.Android 点击手势和长按手势不能一起工作

android - 是否有像素密度为 xxhdpi 的 720dp 设备?

java - 如何使 listFooter 不可点击

php - 更新观看次数,最可靠的方法

java - 理解 NsdChat 示例应用程序的问题