【问题标题】:Android: Custom preference screen size problemAndroid:自定义偏好屏幕尺寸问题
【发布时间】:2011-09-23 13:38:58
【问题描述】:

我正在使用自定义布局来显示首选项,以便可以在屏幕底部插入广告横幅。这个想法是让横幅贴在页面底部,并在屏幕的其余部分以滚动视图的形式显示首选项。 我对偏好所采用的高度有疑问。 我目前在下面的 XML 布局中将其硬编码为“2000dip”。

效果很好,横幅停留在底部,首选项在可滚动视图的顶部。

但是有一个硬编码的高度值是不好的,我希望它自动设置为首选项所采用的确切高度,因为目前它要么太短要么太长(在首选项后面有一个空白区域)。 我试图用 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>

【问题讨论】:

    标签: android scrollview android-linearlayout banner


    【解决方案1】:

    在滚动视图中使用属性“android:fillViewport”使其具有正确的高度。所以这将是解决方案,除了使用此属性,视图不会滚动,或者滚动非常糟糕。在我看来,这像是一个 Android 错误。

    无论如何,有效的解决方案是删除 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>
    

    【讨论】:

      【解决方案2】:

      尝试设置&lt;android:layout_marginRight="10dp" android:layout_marginBottom="10dp" android:layout_marginLeft="5dp&gt;" 在布局属性中。

      【讨论】:

        【解决方案3】:

        为您的外部布局使用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",使其位于广告视图上方。这将根据您的需要对其进行格式化。

        应该可以。如果您有问题,请发表评论。

        【讨论】:

        • 我现在正在尝试使用RelativeLayout。但是这并不能解决 ListView 高度不正确的问题:Banner 正确地卡在了页面底部。滚动视图采用剩余的屏幕高度,好的。但是 ScroillView 中包含的 ListView 与其内容的高度不匹配,并且只在顶部占用了一点空间。问题似乎是 ListView 只占用其初始内容的大小,而在我的帖子的 Java 代码中,我将其更改为填充首选项。 ListView 不会调整大小以适应首选项的全部内容。
        • 尝试将ListView的高度设置为“fill_parent”
        • fill_parent 也没有帮助。但不知何故,我找到了一个解决方案,并将其发布在这里。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-05
        • 2021-03-29
        相关资源
        最近更新 更多