我创建了一个用于绘制图形的类,该类扩展了 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/