我正在開發一款適用於Android平板電腦3.0的應用程序,該應用程序有一個應該可以在橫軸上滾動的活動,就像電子書一樣。在Android中的HorizontalScrollView中動態插入視圖
爲此,我在我的佈局的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'。 –
您確定您將正確的layoutparams和高度寬度設置爲該視圖。需要看看你的'createView()'方法。 – Ronnie