2012-07-31 38 views
0

我在使用這段代碼困難:安卓:如何申請的LayoutParams我的RelativeLayout

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    RelativeLayout layout = (RelativeLayout)findViewById(R.id.RelativeLayout1); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 

我有我的RelativeLayout1在這樣的XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ScrollView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:paddingLeft="60dp" 
    android:paddingTop="53dp" > 

但在我申請的時候scrollview,RelativeLayout1的fill_parent不能像它不是scrollview的子節點時那樣工作。當它不是一個孩子時(即我完全移除滾動視圖),那麼相對佈局覆蓋整個屏幕,因爲我喜歡它。但是當我把它放在scrollview中時,即使我使用了fill_parent,它也只能到達相關佈局中的最後一個元素(一個按鈕)。

所以我試圖通過編程來查看它是否有所作爲,但我似乎無法弄清楚如何將我的params變量應用到我的佈局中的第一位代碼。如果您有任何其他想法,請讓我知道。

回答

1

您忘記了調用setter方法layout.setLayoutParams(params)。 要應用您創建的參數,您必須將它們提供給實際佈局。

當父母是ScrollView時,請謹慎使用fill_parent。隨着「滾動」視圖將其大小調整爲其子級。

+0

謝謝!這看起來不錯。 – capcom 2012-07-31 13:20:03

0

我想通過向scrollview添加一個字段而不是以編程方式執行此操作來解決我的問題。在滾動型,我不得不設置如下:

android:fillViewport="true" 
0

屬性android:fillViewport="true"添加到您的滾動型,如果你想在RelativeLayout的填補了滾動。

見羅曼蓋伊的一篇關於此相同:ScrollView's handy trick

相關問題