我正在为 Android 平板电脑 3.0 开发一个应用程序,该应用程序具有一个可以在水平轴上滚动的活动,例如电子书。
为此,我在布局的 HorizontalScrollView 中使用了 RelativeLayout。这是 XML:
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="800px"
android:id="@+id/horizontalScroll"
android:background="#C6D7D2" android:layout_height="600px">
<RelativeLayout android:id="@+id/container" android:layout_width="fill_parent" android:layout_height="fill_parent">
</RelativeLayout>
</HorizontalScrollView>
此 xml 文件称为 main.xml。
我在java文件中做的是:
setContentView(R.layout.main);
parent = (ViewGroup) findViewById(R.id.container);
View v = createView(); // Thats working for sure.
parent.addView(v);
但它不起作用,视图 V 没有显示在屏幕上。但如果我这样做
addContentView(v)
它将视图 v 添加到屏幕上(证明我的方法有效),但它不可滚动,因为它位于 HorizontalScrollView 之外。我究竟做错了什么?
更新:我试过了,但也没有成功:
setContentView(R.layout.main);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ViewGroup parent = (ViewGroup) findViewById(R.id.container);
View v = new View(this);
v.setBackgroundColor(Color.BLUE);
parent.addView(v,params);
我没有蓝色背景。