2011-09-23 137 views
1

我使用自定義佈局來顯示首選項,以便我可以在屏幕底部插入廣告條。這個想法是讓橫幅貼在頁面的底部,並在滾動視圖中顯示屏幕的其餘部分。 我的偏好設置的高度有問題。 我目前在下面的XML佈局中將其硬編碼爲「2000dip」。Android:自定義首選項屏幕大小問題

它工作正常,橫幅停留在底部,在可滾動視圖頂部的喜好。

但是,硬編碼的高度值不好,我希望它自動設置爲偏好所採用的確切高度,因爲它目前太短或太長(在偏好之後有空白區域)。 我試圖用wrap_content或fill_parent替換硬編碼值,但沒有成功。 有什麼想法?

在我PreferenceActivity我已經包含以下兩行:

setContentView(R.layout.preferences_scrollable); 
    addPreferencesFromResource(R.layout.preferences); 

而且我定義的XML文件preferences_scrollable.xml佈局preferences_scrollable:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <ScrollView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_weight="1" 
     android:id="@+id/pref_scrollable_sv"> 

     <LinearLayout android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:orientation="vertical" 
      android:id="@+id/pref_scrollable_ll"> 

      <ListView android:id="@android:id/list" 
       android:layout_width="fill_parent" android:layout_height="2000dip"> 

      </ListView> 
     </LinearLayout> 
    </ScrollView> 
    <LinearLayout android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:orientation="vertical"> 
     <com.google.ads.AdView android:id="@+id/ad" 
      ads:adSize="BANNER" android:layout_width="fill_parent" 
      ads:loadAdOnCreate="true" ads:adUnitId="xxx" 
      android:layout_height="wrap_content" 
      android:layout_weight="0"/> 
    </LinearLayout> 
</LinearLayout> 

回答

1

在滾動視圖中使用屬性「android:fillViewport」使其具有正確的高度。所以這將是解決方案,除了使用此屬性,視圖不滾動,或滾動非常糟糕。看起來像一個Android的bug對我來說。

無論如何,解決方案的作用是放棄ScrollView,而是使用LinearLayout(支持滾動)。下面給出了正確使用可滾動首選項列表(從Java代碼填充的內容)和底部廣告標題的佈局。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <LinearLayout android:layout_width="fill_parent" 
     android:id="@+id/ad_layout" android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_alignParentBottom="true"> 

     <com.google.ads.AdView android:id="@+id/ad" 
      ads:adSize="BANNER" android:layout_width="fill_parent" 
      ads:loadAdOnCreate="true" ads:adUnitId="xxx" 
      android:layout_height="wrap_content" primaryTextColor="#FFFFFF" 
      secondaryTextColor="#CCCCCC" /> 
    </LinearLayout> 
    <LinearLayout android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:layout_above="@+id/ad_layout"> 

     <ListView android:id="@android:id/list" android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
     </ListView> 
</LinearLayout>  
</RelativeLayout> 
0

嘗試設置<android:layout_marginRight="10dp" android:layout_marginBottom="10dp" android:layout_marginLeft="5dp>" 在佈局屬性中。

+0

nope,沒有做任何事情...... – muslidrikk

0

爲您的外部佈局使用RelativeLayout。例如:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" .....> 

     <LinearLayout android:layout_alignParentBottom="true" android:id="@+id/adsLayout" ...> 
      <!-- This is the ads part of it, which will remain the same as you have above--> 
     </LinearLayout> 
      <!-- You have to have the ads part first, so you are able to reference it with the scrollview --> 
     <ScrollView .... android:layout_above="@+id/adsLayout"> 
      <!-- This ScrollView contents will remain the same --> 
     </ScrollView> 
    </RelativeLayout> 

所以,在佈局中的廣告部分,您使用android:layout_alignParentBottom="true"把它在屏幕的底部。然後在ScrollView中,您放入了android:layout_above="@+id/adsLayout",因此它高於廣告視圖。這將根據您的需要進行格式化。

這應該工作。只是評論,如果你有問題。

+0

我現在試着用RelativeLayout。但是,這並不能解決ListView高度不正確的問題: 橫幅正確地卡在頁面的底部。 滾動視圖佔用剩餘屏幕高度,確定。 但是,ScroillView中包含的ListView與其內容的高度不匹配,只佔用頂部的一點空間。 問題似乎是ListView只取其初始內容的大小,而在我的帖子中的Java代碼中,我將其更改爲填充了首選項。 ListView不會調整大小以適應首選項的全部內容。 – muslidrikk

+0

嘗試設置ListView的高度爲「fill_parent」 – Jakar

+0

fill_parent也沒有幫助。但不知何故,我找到了一個解決方案,並將其發佈在這裏。 – muslidrikk