2011-09-23 139 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