Android:自定义首选项屏幕尺寸问题

发布于 2024-12-06 07:22:49 字数 1920 浏览 1 评论 0 原文

我使用自定义布局来显示首选项,以便我可以在屏幕底部插入广告横幅。这个想法是让横幅粘在页面底部,并在滚动视图中将首选项显示在屏幕的其余部分。 我对偏好所采用的高度有疑问。 目前,我已在下面的 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>

I'm using a custom layout to display preferences so that I can insert an ad banner at the bottom of the screen. The idea is to have the banner stick to the bottom of the page, and have the preferences displayed in the rest of the screen in a scrollview.
I have a problem with the height taken by the preferences.
I've currently hardcoded it to "2000dip" in the XML layout below.

It works fine, the banner stays at the bottom, preferences at the top in a scrollable view.

But having a hardcoded height value is bad and I'd like it to automatically being set to the exact height taken by the preferences because currently it is either too short or too long (with a blank area after the preferences).
I've tried to replace the hardcoded value with wrap_content or fill_parent with no success.
Any idea?

In my PreferenceActivity I've included the following two lines:

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

And I've defined a layout preferences_scrollable in the xml file preferences_scrollable.xml:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好菇凉咱不稀罕他 2024-12-13 07:22:49

在滚动视图中使用属性“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>

Using the attribute "android:fillViewport" in the scrollview makes it have a correct height. So that would be the solution except that with this attribute, the view doesn't scroll, or scrolls very badly. Looks like an Android bug to me.

Anyway, the solution that works is to drop the ScrollView and instead use a LinearLayout (that supports scrolling as well). The layout correctly working with a scrollable preference list (content filled from Java code) on top and an ad banner on the bottom is given below.

<?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>
我为君王 2024-12-13 07:22:49

尝试设置
在布局属性中。

Try to set <android:layout_marginRight="10dp" android:layout_marginBottom="10dp" android:layout_marginLeft="5dp>"
in layout properties.

美人迟暮 2024-12-13 07:22:49

使用相对布局作为外部布局。示例:

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

那应该有效。如果您有问题,请发表评论。

Use a RelativeLayout for your outer layout. Example:

   <?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>

So, in the ads portion of the layout, you use android:layout_alignParentBottom="true" to put it at the bottom of the screen. Then in the ScrollView, you put android:layout_above="@+id/adsLayout", so it goes above the ads view. This will format it as you need.

That should work. Just comment if you have issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文