【发布时间】:2011-08-25 06:35:33
【问题描述】:
我正在为 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);
我没有得到蓝色背景。
【问题讨论】:
-
您是否尝试过将RelativeLayout 更改为水平方向的LinearLayout?
-
你为什么不把你的容器设为
RelativeLayout。 -
您确定为该视图设置了正确的布局参数和高度宽度吗?需要查看您的
createView()方法。 -
@user7777777777 我创建的视图实际上是一个扩展视图的类。我尝试做 parent.addView(v,params);其中 params 是一个 LayoutParams,其中包含设置为 wrap_content 的高度和宽度。所以我只是实例化类型视图的一个对象,然后当我将它添加到父对象时我设置参数
-
在您的更新中,您的视图中没有任何内容 'v' 这就是它不会显示的原因。设置一些高度和宽度而不是
WRAP_CONTENT。
标签: android android-intent horizontalscrollview